Click here to Skip to main content
16,017,351 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello,

I am somewhat familiar with converting using atoi in regards to a string to int:

C#
r.r= atoi(variable.c_str());



But how can I use this in conjuction with .at(i)?

If it matters, the string is a vector.

What I have tried:

C++
for (i = 1; i <= stringVariable.size; i++) {
		

MemberFunction(MemberFunction(atoi(stringVariable.at(i)), variable, variable);

}
Posted
Updated 6-Dec-16 21:00pm
v2
Comments
Philippe Mori 7-Dec-16 10:32am    
Write real code in your example.

To utilise at(n) with atoi() use it thus:

C++
std::vector<std::string> v;

v.push_back("2222");

int i = atoi(v.at(0).c_str());
 
Share this answer
 
Comments
KarstenK 7-Dec-16 3:13am    
the solution is correct, but problematic in complex scenarios like some error handling.
There is a C++ way to do that, try:
C++
#include <iostream>
#include <sstream>
#include <vector>
using namespace std;

int main()
{
  vector < string > v{ "10", "20", "30" };

  for (const auto & x : v)
  {
    istringstream iss(x);
    int n;
    iss >> n;
    cout << n << endl;
  }
}
 
Share this answer
 
v2
Comments
KarstenK 7-Dec-16 3:10am    
Nice and funny ;-)
CPallini 7-Dec-16 3:16am    
Thanks.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900