Parsing String to Array of Integers
I'm looking for a way to convert a string with specified dividers (such as slashes or spaces) into an array of the integers those dividers separate.
For example, if the user inputs 12/3/875/256
, I need to retrieve the array {12, 3, 875, 256}
. Ideally, it would be able to handle an arbitrary length.
I tried sweeping through the string character-by-character and storing ev开发者_StackOverflow中文版erything that's not a divider in a temporary variable, which is added to the array the next time I encounter a divider character. Unfortunately, the type conversions are being a pain in the butt. Is there an easier way to do this?
You can set '/' to a delimiter and read using getline? then you'd have to put each one into a variable, and you'd need to know the size--maybe you can pass over the array and count the slashes? then you'd know that and can set up the array first. You might need to parse each string segment into an int, which may or may not be difficult. (haven't used c++ for a while, I don't remember a convenient way.)
See here for a small example of how this is done (3 posts down).
Try using the boost::tokenizer and boost::lexical_cast
strtok and strtol? (this is somewhat tongue in cheek. Strtok is usually not a good idea)
The splitting is covered in this Parsing String to Array of Integers
COnverting strings to int in C++ has quite a number of relevant questions https://stackoverflow.com/search?q=convert+string+to+int+c%2B%2B
What is the issue with the type conversions? It doesn't seem to be a block as far as I can see.
Can you show your code?
Take a look at this other answer. It even has an example of a tokenizer code using boost::tokenizer.
EDIT:
I copied the code there with the neccessary modifications:
#include <iostream>
#include <string>
#include <boost/foreach.hpp>
#include <boost/tokenizer.hpp>
#include <vector>
#include <boost/lexical_cast.hpp>
#include <iterator>
#include <algorithm>
using namespace std;
using namespace boost;
int main(int argc, char** argv)
{
string text = "125/55/66";
vector<int> vi;
char_separator<char> sep("/");
tokenizer<char_separator<char> > tokens(text, sep);
BOOST_FOREACH(string t, tokens)
{
vi.push_back(lexical_cast<int>(t));
}
copy(vi.begin(), vi.end(), ostream_iterator<int>(cout, "\n"));
}
Will print:
125
55
66
You could use a combination of Boost.split and Boost.lexical_cast to break up the string by whatever delimiters you want, and then you could lexical cast it all.
#include <boost/foreach.hpp>
#include <boost/lexical_cast.hpp>
#include <boost/algorithm/string.hpp>
#include <iostream>
#include <vector>
#include <string>
int main()
{
std::string s = "11/23/2010";
std::vector<std::string> svec;
std::vector<int> ivec;
// split the string 's' on '/' delimiter, and the resulting tokens
// will be in svec.
boost::split(svec, s, boost::is_any_of("/"));
// Simple conversion - iterate through the token vector svec
// and attempt a lexical cast on each string to int
BOOST_FOREACH(std::string item, svec)
{
try
{
int i = boost::lexical_cast<int>(item);
ivec.push_back(i);
}
catch (boost::bad_lexical_cast &ex)
{
std::cout << ex.what();
}
}
return 0;
}
Untested...don't have boost on this machine.
Other ways you could use to convert std::string
/char *
to int
types involve stringstream
use directly, or C constructs like atoi
.
精彩评论