我们可以在c++;在一份声明中? 这是一个愚蠢的问题,但是出于好奇,它可能是在逗号上分割字符串,在字符串上执行函数,然后在一个语句中用C++ + /p>重新加入逗号。
这就是我到目前为止所做的:我们可以在c++;在一份声明中? 这是一个愚蠢的问题,但是出于好奇,它可能是在逗号上分割字符串,在字符串上执行函数,然后在一个语句中用C++ + /p>重新加入逗号。,c++,boost,stl,C++,Boost,Stl,这就是我到目前为止所做的: 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
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, ",");
}
string dostuff(常量字符串&a){
返回字符串(“Foo”);
}
int main(){
字符串s(“a、b、c、d、e、f”);
矢量foobar(100);
转换(boost::make_-token_迭代器(s.begin()、s.end()、boost::char_分隔符(“,”)),
boost::make_-token_迭代器(s.end(),s.end(),boost::char_分隔符(“,”),
foobar.begin(),
boost::bind(&dostuff,_1));
字符串结果=boost::algorithm::join(foobar,“,”);
}
因此,这将导致将“a,b,c,d,e,f”
转换为“Foo,Foo,Foo,Foo,Foo”
我意识到这是OTT,但我只是想扩展我的boost魔法 首先,请注意,您的程序将“Foo,Foo,Foo,Foo,Foo,Foo,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,写入结果字符串——如注释中所述,您希望在那里使用back inserter
至于答案,每当有一个单独的值从一个范围内产生时,我就看<代码> STD::Stult(因为这是C++版本的折叠/缩小)
#包括
#包括
#包括
#包括
#包括
#包括
std::string dostuff(const std::string&a){
返回std::字符串(“Foo”);
}
int main(){
std::字符串s(“a,b,c,d,e,f”);
std::字符串结果=
积累(
++boost::make_-token_迭代器(s.begin()、s.end()、boost::char_分隔符(“,”),
boost::make_-token_迭代器(s.end(),s.end(),boost::char_分隔符(“,”),
dostuff(*boost::make_token_迭代器(s.begin(),s.end(),boost::char_分隔符(“,”),
boost::bind(std::plus(),_1,
绑定(std::plus(),“,”,
bind(dostuff,_2));//或lambda,以获得更好的可读性
标准::coutvoid dostuff(字符串和a){
a=“Foo”;
}
int main()
{
字符串s(“a、b、c、d、e、f”);
向量tmp;
s=boost::join(
(
boost::每个(
boost::split(tmp,s,boost::是(“,”)中的任意一个),
多斯塔夫
),
tmp
),
","
);
返回0;
}
不幸的是,我不能避免两次提到tmp
。也许我以后会想一些事情。好吧,我想这是可能的,但请不要在生产代码中真的这样做
最好是这样
std::string MakeCommaEdFoo(std::string input)
{
std::size_t commas = std::count_if(input.begin(), input.end(),
std::bind2nd(std::equal_to<char>(), ','));
std::string output("foo");
output.reserve((commas+1)*4-1);
for(std::size_t idx = 1; idx < commas; ++idx)
output.append(",foo");
return output;
}
std::string MakeCommaEdFoo(std::string输入)
{
std::size\u t逗号=std::count\u if(input.begin(),input.end(),
std::bind2nd(std::equal_to(),',');
std::字符串输出(“foo”);
输出.储备((逗号+1)*4-1);
用于(std::size\u t idx=1;idx<逗号;++idx)
输出。追加(“,foo”);
返回输出;
}
它不仅性能更好,而且下一个家伙更容易阅读和理解。我实际上正在开发一个库,允许以比迭代器更可读的方式编写代码…不知道我是否能完成这个项目,但似乎死掉的项目往往会堆积在我的计算机上
无论如何,我这里的主要指责显然是迭代器的使用。我倾向于将迭代器视为低级实现细节,当编码时,你几乎不想使用它们
那么,让我们假设我们有一个合适的库:
struct DoStuff { std::string operator()(std::string const&); };
int main(int argc, char* argv[])
{
std::string const reference = "a,b,c,d,e,f";
std::string const result = boost::join(
view::transform(
view::split(reference, ","),
DoStuff()
),
","
);
}
视图的概念是作为另一个容器的光包装:
- 从用户的角度来看,它的行为类似于容器(减去实际修改容器结构的操作)
- 从实现的角度来看,它是一个轻量级对象,包含尽可能少的数据-->这里的值是短暂的,并且只有迭代器存在的时间才有效
我已经有了转换
部分工作,我想知道拆分
如何工作(一般来说),但我想我会进入它;)我必须读两遍,才能发现这个问题隐藏在标题中。(提示).BTW:您应该使用back_inserter输出迭代器,并从大小为0的向量开始。我假设您的意思是“仅使用boost和标准库实用程序”?否则,我们可以定义一个合适的函数。这将生成foo,foo,foo,foo,foo,foo,
,而不是foo,foo,foo,foo,foo
(请注意末尾的额外逗号)@比利·奥尼尔:哦..是的。我想这取决于boost.range的熟练程度来回答一行。当我看到脚本语言时,我就是这么想的。你确定先计算逗号更有效吗?在一般情况下(对孤立字符串实际应用谓词)“我担心这更像是一种阻碍。@Matthieu:实际上应该没有什么区别。它会表现得更好,因为a.你不需要构造令牌迭代器,B.(更重要的是)你一次只使用一个缓冲区。这将导致缓存压力降低,因为两个字符串中只有一个(源代码和正在构建的代码)在任何一点上都需要“靠近处理器”。这也比在最后一项中添加逗号并在以后删除它要好(原因应该很明显)假设您没有实际读取输入字符串。即使问题过于简单,OP的谓词也会在输入中使用字符串。我冒昧地重新格式化了代码,现在我明白了发生的事情,我想知道这是否真的有效。困扰我的是join
的第一个参数:您不仅使用tmp
两次,你也在逗号的两侧使用它。我知道它只在一侧被修改,但我想知道一个错误是否可能是因为逗号没有定义序列点。它是一个逗号运算符,所以它定义了序列点。@ybungalobill:那我错了!我还是pe
std::string MakeCommaEdFoo(std::string input)
{
std::size_t commas = std::count_if(input.begin(), input.end(),
std::bind2nd(std::equal_to<char>(), ','));
std::string output("foo");
output.reserve((commas+1)*4-1);
for(std::size_t idx = 1; idx < commas; ++idx)
output.append(",foo");
return output;
}
struct DoStuff { std::string operator()(std::string const&); };
int main(int argc, char* argv[])
{
std::string const reference = "a,b,c,d,e,f";
std::string const result = boost::join(
view::transform(
view::split(reference, ","),
DoStuff()
),
","
);
}