How to convert string to long int

Hi everyone, I need to declare a vector using string and then convert it to long int.
I wrote the following code but it is showing error.

1
2
3
4
5
6
7
  string state = default("1 2 3 4 5");
  std::vector<std::string> statesStr = cStringTokenizer(par("state")).asVector();
               std::vector<long> statesPar;
               for (auto k : statesStr) {
                   // How to convert the string to long?
                   statesPar.push_back(k.c_str()); // error
               }


After searching, i found that it can be done using std::stol(), but i do not know how to code it.

Would anyone please help me with this? Thank you.

Not even after looking at examples in the reference?
https://www.cplusplus.com/reference/string/stol/

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <vector>
#include <string>
#include <iostream>
#include <exception>

int main()
{
	const std::vector<std::string> statesStr {"1", "!", "3", "4", "5"};
	std::vector<long> statesPar;

		for (const auto& k : statesStr)
			try {
				statesPar.push_back(std::stol(k));
			}
			catch (const std::invalid_argument& a) {
				std::cout << "Cannot convert '" << k << "'\n";
			}

	for (const auto v : statesPar)
		std::cout << v << ' ';

	std::cout << '\n';
}


Note that if no conversion can be performed, then an exception is thrown - hence the try/catch.
Last edited on
@salem, i think, still i am doing it wrong. I added below lines, but still it is showing error

1
2
3
4
5
6
7
8
9
10

std::vector<std::string> statesStr = cStringTokenizer(par("state")).asVector();
               std::vector<long> statesPar;
               for (auto k : statesStr) {
                  
                   std::string::size_type sz;
                   long s = std::stol (statesStr, &sz);

                   statesPar.push_back(s);
               }

Can anyone please suggest me the code?
Last edited on
However, for a 'simpler' way of converting a string of numbers to a vector of long, consider:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <vector>
#include <string>
#include <iostream>
#include <cstdlib>

int main()
{
	const std::string state {"1 2 3 4 5"};

	std::vector<long> statesPar;

	for (const char *nxt = state.data(), *pt; (*(pt = nxt)); )
		statesPar.push_back(std::strtol(pt, (char**)&nxt, 10));

	for (const auto v : statesPar)
		std::cout << v << ' ';

	std::cout << '\n';
}

1. Study seeplus's example.

2. Put debug code in your code to see if you're actually trying to convert what you think.

3. Level up and start using a debugger to achieve step 2.

1
2
3
4
5
6
7
8
9
std::vector<std::string> statesStr = cStringTokenizer(par("state")).asVector();
               std::vector<long> statesPar;
               for (auto k : statesStr) {
                   std::cout << "DEBUG: " << k << std::endl;
                   std::string::size_type sz;
                   long s = std::stol (statesStr, &sz);  //!! are you getting error messages?!?

                   statesPar.push_back(s);
               }

Better variable names might clear things up also.

@seeplus and @salem, many thanks.

Very helpful answers
Topic archived. No new replies allowed.