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.
例如,如果用户输入12/3/875/256,我需要检索数组{12,3,875,256}。理想情况下,它可以处理任意长度。
I tried sweeping through the string character-by-character and storing everything 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?
我尝试遍历每个字符的字符串,并在一个临时变量中存储不是分隔符的所有内容,在下次遇到分隔符时,这个变量将被添加到数组中。不幸的是,这种类型的转换是痛苦的。有更简单的方法吗?
5 个解决方案
#1
2
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.)
您可以将'/'设置为分隔符,并使用getline进行读取?然后你需要把每一个都放到一个变量中,你需要知道它的大小——也许你可以把数组传递过去,数一下斜杠?然后你就知道了,可以先设置数组。您可能需要将每个字符串段解析为int,这可能很难,也可能不困难。(我已经有一段时间没有使用c++了,我不记得有什么方便的方法了。)
See here for a small example of how this is done (3 posts down).
请看这里的一个小例子,说明这是如何实现的(下面有3篇文章)。
#2
#3
1
strtok and strtol? (this is somewhat tongue in cheek. Strtok is usually not a good idea)
strtok strtol ?)这是在开玩笑。Strtok通常不是个好主意)
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://*.com/search?q=convert+string+to+int+c%2B%2B
在c++中,将字符串转换为int有相当多的相关问题,https://*.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?
你能展示你的代码吗?
#4
1
Take a look at this other answer. It even has an example of a tokenizer code using boost::tokenizer.
看看另一个答案。它甚至还有一个使用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
#5
0
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.
你可以结合使用Boost。分裂和促进。lexical_cast可以使用任何分隔符来分隔字符串,然后可以使用lexical_cast将其全部转换为lexical。
#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
.
其他可以将std::string/char *转换为int类型的方法包括直接使用stringstream,或者像atoi这样的C结构。
#1
2
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.)
您可以将'/'设置为分隔符,并使用getline进行读取?然后你需要把每一个都放到一个变量中,你需要知道它的大小——也许你可以把数组传递过去,数一下斜杠?然后你就知道了,可以先设置数组。您可能需要将每个字符串段解析为int,这可能很难,也可能不困难。(我已经有一段时间没有使用c++了,我不记得有什么方便的方法了。)
See here for a small example of how this is done (3 posts down).
请看这里的一个小例子,说明这是如何实现的(下面有3篇文章)。
#2
#3
1
strtok and strtol? (this is somewhat tongue in cheek. Strtok is usually not a good idea)
strtok strtol ?)这是在开玩笑。Strtok通常不是个好主意)
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://*.com/search?q=convert+string+to+int+c%2B%2B
在c++中,将字符串转换为int有相当多的相关问题,https://*.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?
你能展示你的代码吗?
#4
1
Take a look at this other answer. It even has an example of a tokenizer code using boost::tokenizer.
看看另一个答案。它甚至还有一个使用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
#5
0
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.
你可以结合使用Boost。分裂和促进。lexical_cast可以使用任何分隔符来分隔字符串,然后可以使用lexical_cast将其全部转换为lexical。
#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
.
其他可以将std::string/char *转换为int类型的方法包括直接使用stringstream,或者像atoi这样的C结构。