C ++提振字符串分割 [英] c++ boost split string

查看:142
本文介绍了C ++提振字符串分割的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用boost :: split()方法来分割字符串作为这样的:

I am using boost::split() method to split a string as this:

我首先要确保包括正确的头有机会获得提振::分裂:

I first make sure to include the correct header to have access to boost::split:

#include <boost/algorithm/string.hpp>

然后

vector<string> strs;
boost::split(strs,line,boost::is_any_of("\t"));

和线条像

   "test    test2   test3"

这是我如何消费的结果字符串的vector

This is how i consume the result string vector

void printstrs(vector<string> strs){
    for(vector<string>::iterator it = strs.begin();it!=strs.end();++it){
        cout<<*it<< "-------";
    }
    cout<<endl;
}

但为什么在结果可疑交易报告我只得到TEST2和TEST3,不应该是测试,TEST2和TEST3,有\\字符串中T(标签)。

but why in the result strs I only get "test2" and "test3", shouldn't be "test","test2" and "test3", there are \t(tab) in the string.

感谢

更新@Apr月24日,2011年,这似乎后,我改变code的一行在prinstr我可以看到第一个字符串。我换

updated @Apr 24th,2011, It seemed after I changed one line of code at prinstr I can see the first string. I changed

cout<<*it<< "-------";

cout<<*it<<endl;

和它似乎-------覆盖的第一个字符串弄好了。

And it seemed "-------" covered the first string somehow.

谢谢大家。

推荐答案

问题是别的地方在你的code,因为这个作品:

The problem is somewhere else in your code, because this works:

string line("test\ttest2\ttest3");
vector<string> strs;
boost::split(strs,line,boost::is_any_of("\t"));

cout << "* size of the vector: " << strs.size() << endl;    
for (size_t i = 0; i < strs.size(); i++)
    cout << strs[i] << endl;

和测试你的方法,它采用了矢量迭代器也可以工作:

and testing your approach, which uses a vector iterator also works:

string line("test\ttest2\ttest3");
vector<string> strs;
boost::split(strs,line,boost::is_any_of("\t"));

cout << "* size of the vector: " << strs.size() << endl;
for (vector<string>::iterator it = strs.begin(); it != strs.end(); ++it)
{
    cout << *it << endl;
}

同样,你的问题是其他地方。也许你认为什么是 \\ t 上串字符,是不是。我将填补code。与(1)调试,通过监控向量的插入,以确保一切开始被插入它应该是这样。

Again, your problem is somewhere else. Maybe what you think is a \t character on the string, isn't. I would fill the code with debugs, starting by monitoring the insertions on the vector to make sure everything is being inserted the way its supposed to be.

输出:

* size of the vector: 3
test
test2
test3

这篇关于C ++提振字符串分割的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆