vector even positions

HI I have a .txt file that has a data that i have read and saved inside a vector container. now those data saved has a mixture of strings and integers. by the way the vector is declared as vector<string> vect;

meaning
 
  data1:data2:data:data4:data5:data6........


all the data position in the odd numbers are strings, all the data position in even are integers. what i want to do is to add the data saved inside the vector but only the ones in the even positions. is that possible ?
Use a for a loop and on an even index use stoi to convert your string into an int.
http://www.cplusplus.com/reference/string/stoi/
Example
1
2
3
4
5
6
7
8
9
10
 vector<string> data = { "1", "Bla", "2", "Bla Bla", "3", "Dummy"};

  for (size_t i = 0; i < data.size(); i++)
  {
    if (i % 2 == 0) // even
    {
      int num = stoi(data[i]);
      // use num
    }
  }
ohhhh i think my compiler does not support c++11, i am getting

stoi’ was not declared in this scope
I have found another way to convert, the below is the code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
        while(getline(tree,readis,':'))
	{
		test.push_back(readis);	
	}
	for(int y=1; y<test.size(); y+=2)
	{
	if(test.empty())
	{
		cout<<"Your Vector is empty!!"<<endl;
	}
	else
	{
		s1 = strtod(test[y]);
 	
		
	}
	}


but seems like i am having error, s1 is a double

1
2
3
 error: cannot convert ‘std::basic_string<char>’ to ‘const char*’ for argument ‘1’ to ‘double strtod(const char*, char**)’
   s1 = strtod(test[y]);

Last edited on
What compiler do you use?
Maybe you have to use the old atoi function then.
http://www.cplusplus.com/reference/cstdlib/atoi/
The reason is because test[y] is an std::string, but the argument list is expecting a const char*. So, you have to do strtod(test[y].c_str());
thanks thanks you guys really helped a lot. much appreciated
Topic archived. No new replies allowed.