如何从向量字符串中获取并集字符串?
我有一个填充了一些文件扩展名的向量字符串,如下所示:
vector<string> vExt;
vExt.push_back("*.JPG;*.TGA;*.TIF");
vExt.push_back("*.PNG;*.RAW");
vExt.push_back("*.BMP;*.HDF");
vExt.push_back("*.GIF");
vExt.push_back("*.JPG");
vExt.push_back("*.BMP");
我现在想从上述向量字符串中获取并集的字符串,其中每个文件扩展名在结果字符串中必须是唯一的。对于我给定的示例,生成的字符串应采用 "*.JPG;*.TGA;*.TIF;*.PNG;*.RAW;*.BMP;*.HDF;*.GIF" 的形式
。
我知道 std::unique 可以删除范围内的连续重复项。它不适合我的条件。您能告诉我该怎么做吗?谢谢你!
I have a vector string filled with some file extensions as follows:
vector<string> vExt;
vExt.push_back("*.JPG;*.TGA;*.TIF");
vExt.push_back("*.PNG;*.RAW");
vExt.push_back("*.BMP;*.HDF");
vExt.push_back("*.GIF");
vExt.push_back("*.JPG");
vExt.push_back("*.BMP");
I now want to get a string of union set from the above-mentioned vector string, in which each file extension must be unique in the resulting string. As for my given example, the resulting string should take the form of "*.JPG;*.TGA;*.TIF;*.PNG;*.RAW;*.BMP;*.HDF;*.GIF"
.
I know that std::unique can remove consecutive duplicates in range. It con't work with my condition. Would you please show me how to do that? Thank you!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在这里查看它:http://ideone.com/0fmy0(FIXED)
输出:
See it live here: http://ideone.com/0fmy0 (FIXED)
output:
我将 将每个字符串标记为组成部分(使用分号作为分隔符),并将生成的标记粘贴到一个集合中。该集合的结果内容就是您正在寻找的内容。
I'd tokenize each string into constituent parts (using semicolon as the separator), and stick the resulting tokens into a set. The resultant contents of that set is what you're looking for.
您需要解析包含多个文件扩展名的字符串,然后将它们推入向量中。之后
std::unique
将执行您想要的操作。看一下 Boost.Tokenizer 类,这应该会让这变得微不足道。You need to parse the strings that contain multiple file extensions and then push them into the vector. After that
std::unique
will do what you want. Have a look at the Boost.Tokenizer class, that should make this trivial.