问题
I need some help with getting all the integers from a std::string and getting each of those integer into an int variable.
String example:
<blah> hi 153 67 216
I would like the program to ignore the "blah" and "hi" and store each of the integers into an int variable. So it comes out to be like:
a = 153
b = 67
c = 216
Then i can freely print each separately like:
printf("First int: %d", a);
printf("Second int: %d", b);
printf("Third int: %d", c);
Thanks!
回答1:
You can create your own function that manipulates a std::ctype facet by using its scan_is method. Then you can return the generated string to a stringstream object and insert the contents to your 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
回答2:
- Use
std::isdigit()to check whether a character is a digit. Until space is not reached, add stepwise to a separatestring. - Then use
std::stoi()to convert your string to an int. - Clear the content of the string using
clear()method. - Go to first step
Repeat until the end of the base string is not reached.
回答3:
First use 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 shouldn't use single variables a b c, but an array like int input[200], or better, a std::vector, which can adapt to the number of elements you 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 have to:
#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 C++ you might want to avoid using printf, and prefer std::cout
回答4:
#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];
}
}
回答5:
To read a line of string and extract integer from it,
getline(cin,str);
stringstream stream(str);
while(1)
{
stream>>int_var_arr[i];
if(!stream)
break;
i++;
}
}
the integers are stored in int_var_arr[]
来源:https://stackoverflow.com/questions/17331250/read-all-integers-from-string-c