Read all integers from C ++ string

I need help getting all the integers from std :: string and getting each of these integers in an int variable.

Example line:

<blah> hi 153 67 216 

I would like the program to ignore "blah" and "hi" and store each of the integers in an int variable. It so happened that:

 a = 153 b = 67 c = 216 

Then I can freely print each separately, for example:

 printf("First int: %d", a); printf("Second int: %d", b); printf("Third int: %d", c); 

Thanks!

+4
source share
5 answers

You can create your own function that manipulates the std::ctype facet using your scan_is method. Then you can return the generated string to a stringstream object and paste the contents into integers:

 #include <iostream> #include <locale> #include <string> #include <sstream> #include <algorithm> #include <iterator> #include <cstring> std::string extract_ints(std::ctype_base::mask category, std::string str, std::ctype<char> const& facet) { using std::strlen; char const *begin = &str.front(), *end = &str.back(); auto res = facet.scan_is(category, begin, end); begin = &res[0]; end = &res[strlen(res)]; return std::string(begin, end); } std::string extract_ints(std::string str) { return extract_ints(std::ctype_base::digit, str, std::use_facet<std::ctype<char>>(std::locale(""))); } int main() { int a, b, c; std::string str = "abc 1 2 3"; std::stringstream ss(extract_ints(str)); ss >> a >> b >> c; std::cout << a << '\n' << b << '\n' << c; } 

Output:

1 2 3

Demo

+8
source
  • Use std::isdigit() to check if the character is a digit. Until space is reached, add in stages to a separate string .
  • Then use std::stoi() to convert your string to int.
  • Clear the contents of a string using the clear() method.
  • Go to the first step

Repeat until the end of the baseline is reached.

+1
source

First use a string tokenizer

 std::string text = "token, test 153 67 216"; char_separator<char> sep(", "); tokenizer< char_separator<char> > tokens(text, sep); 

Then, if you do not know exactly how many values ​​you will get, you should not use single abc variables, but an array, for example int input[200] , or better, std::vector , which can adapt to the number of elements read.

 std::vector<int> values; BOOST_FOREACH (const string& t, tokens) { int value; if (stringstream(t) >> value) //return false if conversion does not succeed values.push_back(value); } for (int i = 0; i < values.size(); i++) std::cout << values[i] << " "; std::cout << std::endl; 

You must:

 #include <string> #include <vector> #include <sstream> #include <iostream> //std::cout #include <boost/foreach.hpp> #include <boost/tokenizer.hpp> using boost::tokenizer; using boost::separator; 

By the way, if you are programming with C ++, you may need to avoid using printf and prefer std::cout

+1
source
 #include <iostream> #include <string> #include <sstream> #include <algorithm> #include <iterator> #include <vector> int main() { using namespace std; int n=8,num[8]; string sentence = "10 20 30 40 5 6 7 8"; istringstream iss(sentence); vector<string> tokens; copy(istream_iterator<string>(iss), istream_iterator<string>(), back_inserter(tokens)); for(int i=0;i<n;i++){ num[i]= atoi(tokens.at(i).c_str()); } for(int i=0;i<n;i++){ cout<<num[i]; } } 
0
source

To read a line of a line and extract an integer from it,

  getline(cin,str); stringstream stream(str); while(1) { stream>>int_var_arr[i]; if(!stream) break; i++; } } 

integers are stored in int_var_arr []

0
source

Source: https://habr.com/ru/post/1488380/


All Articles