基于特定分隔符拆分字符串的最有效的c++方法是什么?类似于python中的split方法
What is the most efficient C++ method to split a string based on a particular delimiter similar to split method in python?
getline(cin,s);
istringstream iss(s);
do
{
string sub;
iss>>sub;
q.insert(sub);
}while(iss);
当question想要我在空间的基础上进行分割时,我使用了这种技术,所以谁能告诉我当有一个特定的分隔符,如';'或':'时,如何分割?
有人告诉我关于strtok函数,但我不能得到它的用法,所以如果有人能帮助,那就太好了。
首先,不要使用strtok
。。
在标准库中并没有这样的函数。我使用如下:
std::vector<std::string>
split( std::string const& original, char separator )
{
std::vector<std::string> results;
std::string::const_iterator start = original.begin();
std::string::const_iterator end = original.end();
std::string::const_iterator next = std::find( start, end, separator );
while ( next != end ) {
results.push_back( std::string( start, next ) );
start = next + 1;
next = std::find( start, end, separator );
}
results.push_back( std::string( start, next ) );
return results;
}
我相信Boost有很多这样的函数。(我实现我的大部分都是在Boost出现之前。)
相关文章:
- 为不同配置设置MSVC_RUNTIME_LIBRARY的正确方法是什么
- 在C++中,将大的无符号浮点数四舍五入为整数的最佳方法是什么
- 实现无开销push_back的最佳方法是什么
- C++从另一个类访问公共静态向量的正确方法是什么
- 在 c++ 中拥有一组结构的正确方法是什么?
- 通过JNI传递数据数组的最快方法是什么
- 用常见虚拟函数实现的任意组合来实现派生类的正确方法是什么
- 使用不同的CRT将新的C++代码与旧的(二进制)组件隔离开来的最佳方法是什么
- 当无法使用模板和宏时,生成类型变体C++代码的最简单方法是什么?
- 在另一个类视图中添加最多2个图表的正确方法是什么
- 在C++中样板"冷/never_inline"错误处理技术的最佳方法是什么?
- 在 c++ 中对类中的 c 字符串动态数组进行排序的最佳方法是什么?
- 在C++中包含原型文件的正确方法是什么?
- 在 OpenCV C++ 中估计基本矩阵之前对相应点进行归一化的正确方法是什么?
- 在PostgreSQL中根据它们的ID选择大量行的最快方法是什么?
- 在OSX上使用CMake将Adobe的XMP工具包构建为共享库的最简单方法是什么?
- 将一系列整数放入类的最佳方法是什么?
- 从长整整转换为uint64_t的推荐方法是什么?
- 将此布尔值传递给此函数的最有效方法是什么?
- 通过比较C++中的行在 txt 文件中搜索的最简单方法是什么?