【发布时间】:2011-12-30 03:10:46
【问题描述】:
我以前问过这个问题,但信息比现在少。
我基本上拥有的是一个 char 类型的数据块。该块包含我需要格式化并放入向量中的文件名。我最初认为这个字符块的形成在每个文件名之间有三个空格。现在,我意识到它们是 '/0' 以空字符结尾的字符。因此,当我认为存在空格而不是空字符时,所提供的解决方案非常适合我给出的示例。
这是结构的样子。另外,我应该指出我确实有字符数据块的大小。
filename1.bmp/0/0/0brick.bmp/0/0/0toothpaste.gif/0/0/0
最好的解决方案是这样的:
// The stringstream will do the dirty work and deal with the spaces.
std::istringstream iss(s);
// Your filenames will be put into this vector.
std::vector<std::string> v;
// Copy every filename to a vector.
std::copy(std::istream_iterator<std::string>(iss),
std::istream_iterator<std::string>(),
std::back_inserter(v));
// They are now in the vector, print them or do whatever you want with them!
for(int i = 0; i < v.size(); ++i)
std::cout << v[i] << "\n";
这对我原来的问题非常有用,但不是因为它们是空字符而不是空格。有什么办法可以使上面的例子工作。我尝试用空格替换数组中的空字符,但这没有用。
关于将此字符块格式化为字符串向量的最佳方法有什么想法吗?
谢谢。
【问题讨论】: