我们可以在 c++ 中拆分、操作和重新连接字符串吗?在一份声明中?
这是一个有点愚蠢的问题,但出于好奇,是否有可能用 C++ 在一个语句中用逗号分割字符串,对字符串执行一个函数,然后用逗号重新连接它?
这就是我到目前为止所拥有的:
string dostuff(const string& a) {
return string("Foo");
}
int main() {
string s("a,b,c,d,e,f");
vector<string> foobar(100);
transform(boost::make_token_iterator<string>(s.begin(), s.end(), boost::char_separator<char>(",")),
boost::make_token_iterator<string>(s.end(), s.end(), boost::char_separator<char>(",")),
foobar.begin(),
boost::bind(&dostuff, _1));
string result = boost::algorithm::join(foobar, ",");
}
所以这将导致将 "a,b,c,d,e,f"
变成 "Foo,Foo,Foo,Foo,Foo,Foo “
我意识到这是 OTT,但只是想扩展我的加速魔法。
This is a bit of a daft question, but out of curiousity would it be possibly to split a string on comma, perform a function on the string and then rejoin it on comma in one statement with C++?
This is what I have so far:
string dostuff(const string& a) {
return string("Foo");
}
int main() {
string s("a,b,c,d,e,f");
vector<string> foobar(100);
transform(boost::make_token_iterator<string>(s.begin(), s.end(), boost::char_separator<char>(",")),
boost::make_token_iterator<string>(s.end(), s.end(), boost::char_separator<char>(",")),
foobar.begin(),
boost::bind(&dostuff, _1));
string result = boost::algorithm::join(foobar, ",");
}
So this would result in turning "a,b,c,d,e,f"
into "Foo,Foo,Foo,Foo,Foo,Foo"
I realise this is OTT, but was just looking to expand my boost wizardry.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
首先,请注意您的程序写入“Foo,Foo,Foo,Foo,Foo,Foo,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,, ,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,, ,,,,,,,,,,,,,”到你的结果字符串 - 正如评论中已经提到的,你想在那里使用 back_inserter 。
至于答案,每当某个范围产生单个值时,我都会查看 std::accumulate(因为这是折叠/减少的 C++ 版本)
除了现在它远远超过顶部并重复 make_token_iterator 两次。我猜 boost.range 获胜。
First, note that your program writes "Foo,Foo,Foo,Foo,Foo,Foo,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,," to your result string -- as already mentioned in comments, you wanted to use back_inserter there.
As for the answer, whenever there's a single value resulting from a range, I look at
std::accumulate
(since that is the C++ version of fold/reduce)Except now it's way over the top and repeats make_token_iterator twice. I guess boost.range wins.
不幸的是,我无法避免两次提及
tmp
。也许我稍后会想到一些事情。Unfortunately I can't eliminate mentioning
tmp
twice. Maybe I'll think of something later.我实际上正在开发一个库,允许以比单独迭代器更可读的方式编写代码...不过不知道我是否会完成该项目,似乎死项目往往会在我的计算机上累积...
无论如何我在这里的主要责备显然是迭代器的使用。我倾向于将迭代器视为低级实现细节,在编码时您很少想使用它们。
因此,假设我们有一个合适的库:
视图的想法是成为另一个容器周围的轻包装器:
我已经让
transform
部分正常工作,我想知道split
是如何工作的(一般来说),但我想我会进入它;)I am actually working on a library to allow writing code in a more readable fashion than iterators alone... don't know if I'll ever finish the project though, seems dead projects tend to accumulate on my computer...
Anyway the main reproach I have here is obviously the use of iterators. I tend to think of iterators as low-level implementation details, when coding you rarely want to use them at all.
So, let's assume that we have a proper library:
The idea of a view is to be a lightwrapper around another container:
I already have the
transform
part working, I am wondering how thesplit
could work (generally), but I think I'll get into it ;)好吧,我想这是可能的,但是请不要在生产代码中这样做。
更好的是像这样的东西,
它不仅性能更好,而且下一个人更容易阅读和理解。
Okay, I guess it's possible, but please please don't really do this in production code.
Much better would be something like
Not only will it perform better, it will is much easier for the next guy to read and understand.