逗号分隔令牌到const char **的向量 [英] Vector of comma separated token to const char**
问题描述
我正在尝试将逗号分隔的字符串转换为const char *的向量.使用以下代码,按预期输出是
I am trying to convert a comma separated string to vector of const char*. With the following code, by expected output is
ABC_
DEF
HIJ
但我明白了
HIJ
DEF
HIJ
我要去哪里错了?
代码:
#include <iostream>
#include <boost/tokenizer.hpp>
#include <vector>
#include <string>
using namespace std;
int main()
{
string s("ABC_,DEF,HIJ");
typedef boost::char_separator<char> char_separator;
typedef boost::tokenizer<char_separator> tokenizer;
char_separator comma(",");
tokenizer token(s, comma);
tokenizer::iterator it;
vector<const char*> cStrings;
for(it = token.begin(); it != token.end(); it++)
{
//cout << (*it).c_str() << endl;
cStrings.push_back((*it).c_str());
}
std::vector<const char*>::iterator iv;
for(iv = cStrings.begin(); iv != cStrings.end(); iv++)
{
cout << *iv << endl;
}
return 0;
}
在以下答案的帮助下的解决方案: (PaulMcKenzie使用列表提供了一种更整洁的解决方案.)
Solution with help of answers below: (PaulMcKenzie offers a neater solution using lists.)
#include <iostream>
#include <boost/tokenizer.hpp>
#include <vector>
#include <string>
using namespace std;
char* createCopy(std::string s, std::size_t bufferSize)
{
char* value = new char[bufferSize];
memcpy(value, s.c_str(), (bufferSize - 1));
value[bufferSize - 1] = 0;
return value;
}
int main()
{
string s("ABC_,DEF,HIJ");
typedef boost::char_separator<char> char_separator;
typedef boost::tokenizer<char_separator> tokenizer;
char_separator comma(",");
tokenizer token(s, comma);
tokenizer::iterator it;
vector<const char*> cStrings;
for(it = token.begin(); it != token.end(); it++)
{
//cout << it->c_str() << endl;
cStrings.push_back(createCopy(it->c_str(),
(it->length() + 1)));
}
std::vector<const char*>::iterator iv;
for(iv = cStrings.begin(); iv != cStrings.end(); iv++)
{
cout << *iv << endl;
}
//delete allocations by new
//...
return 0;
}
推荐答案
这就是问题:boost::tokenizer::iterator
不会返回字符串副本的所有权,而是对内部副本的引用.
Here's the thing: boost::tokenizer::iterator
doesn't return you ownership of a copy of the string, but a refernce to an internal copy.
例如,运行代码后,我得到:
For example, after running your code I get:
HIJ
HIJ
HIJ
解决方案是将cStrings.push_back((*it).c_str())
替换为以下其中一项:
the solution is to replace cStrings.push_back((*it).c_str())
with one of the following:
char* c = new char[it->length() + 1];
c[it->length()] = 0;
cStrings.push_back(c);
std::strncpy(c, it->c_str(), it->length());
看起来不漂亮,但是您可能不会比这更快(至少如果您想使用boost::tokenizer
.
doesn't look pretty, but you probably won't get faster than that (at least if you want to use boost::tokenizer
.
另一种选择是将boost::tokenizer
完全替换为例如strtok
-可以在此处找到示例: C拆分char数组放入不同的变量
other option is to totally replace boost::tokenizer
with e.g. strtok
- an example can be found here: C split a char array into different variables
您也可以使用boost::algorithm::string::split
,但是稍后可能需要将string
重新映射到const char*
.
you can also use boost::algorithm::string::split
, but then you might need to remap string
to const char*
later on.
这篇关于逗号分隔令牌到const char **的向量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!