gpt4 book ai didi

c++ - std::accumulate 有引用吗?

转载 作者:行者123 更新时间:2023-12-01 17:32:41 24 4
gpt4 key购买 nike

尝试“滥用”std::accumulate 算法(为什么它出现在“数字” header 中?;))

template<class Range, class Seperator>
std::string strjoin(Range&& range, Seperator&& seperator) {
if (std::empty(range)) {
return "";
}
return std::accumulate(std::begin(range) + 1, std::end(range), std::ostringstream{} << *std::begin(range), [seperator](auto&& lhs, auto&& rhs) { return lhs << seperator << rhs; }).str();
}

Compiler explorer

上面的内容无法编译,并且是一种有点“幼稚”的形式,但我认为它突出了“问题”。

来自编译器资源管理器链接的错误:

<source>(13): error C2280: 'std::basic_ostream<char,std::char_traits<char>>::basic_ostream(const std::basic_ostream<char,std::char_traits<char>> &)': attempting to reference a deleted function

C:/data/msvc/14.22.27905/include\ostream(57): note: see declaration of 'std::basic_ostream<char,std::char_traits<char>>::basic_ostream'

C:/data/msvc/14.22.27905/include\ostream(57): note: 'std::basic_ostream<char,std::char_traits<char>>::basic_ostream(const std::basic_ostream<char,std::char_traits<char>> &)': function was explicitly deleted

C:/data/msvc/14.22.27905/include\numeric(72): note: see reference to function template instantiation 'std::basic_ostream<char,std::char_traits<char>> strjoin::<lambda_d85bd1f8d5f8c34784482fe135d50702>::operator ()<std::basic_ostringstream<char,std::char_traits<char>,std::allocator<char>>,int&>(std::basic_ostringstream<char,std::char_traits<char>,std::allocator<char>> &&,int &) const' being compiled

<source>(13): note: see reference to function template instantiation '_Ty std::reduce<std::_Vector_iterator<std::_Vector_val<std::_Simple_types<int>>>,_Ostr,strjoin::<lambda_d85bd1f8d5f8c34784482fe135d50702>>(const _InIt,const _InIt,_Ty,_BinOp)' being compiled

with

[

_Ty=std::ostringstream,

_Ostr=std::ostringstream,

_InIt=std::_Vector_iterator<std::_Vector_val<std::_Simple_types<int>>>,

_BinOp=strjoin::<lambda_d85bd1f8d5f8c34784482fe135d50702>

]

<source>(19): note: see reference to function template instantiation 'std::string strjoin<std::vector<int,std::allocator<int>>&,const char(&)[4]>(Range,Seperator)' being compiled

with

[

Range=std::vector<int,std::allocator<int>> &,

Seperator=const char (&)[4]

]

C:/data/msvc/14.22.27905/include\numeric(72): error C2679: binary '=': no operator found which takes a right-hand operand of type 'std::basic_ostream<char,std::char_traits<char>>' (or there is no acceptable conversion)

C:/data/msvc/14.22.27905/include\sstream(488): note: could be 'std::basic_ostringstream<char,std::char_traits<char>,std::allocator<char>> &std::basic_ostringstream<char,std::char_traits<char>,std::allocator<char>>::operator =(const std::basic_ostringstream<char,std::char_traits<char>,std::allocator<char>> &)'

C:/data/msvc/14.22.27905/include\sstream(468): note: or 'std::basic_ostringstream<char,std::char_traits<char>,std::allocator<char>> &std::basic_ostringstream<char,std::char_traits<char>,std::allocator<char>>::operator =(std::basic_ostringstream<char,std::char_traits<char>,std::allocator<char>> &&)'

C:/data/msvc/14.22.27905/include\numeric(72): note: while trying to match the argument list '(_Ty, std::basic_ostream<char,std::char_traits<char>>)'

with

[

_Ty=std::ostringstream

]

Compiler returned: 2
<小时/>

为那些对如何使用普通 for 循环编写算法感兴趣的人提供的代码片段:

template<class Range, class Seperator>
std::string strjoin(Range&& range, Seperator&& seperator) {
if (!std::empty(range)) {
std::ostringstream oss{};
oss << *std::begin(range);
for (auto&& e : iter_range(std::begin(range) + 1, std::end(range))) {
oss << seperator << e;
}
return oss.str();
}
return "";
}

最佳答案

std::accumulate要求“累加器”(第三个参数)为 CopyAssignableCopyConstructiblestd::ostringstream 两者都不是,所以你不能使用它。

您将需要使用不同的方法,例如 std::for_each,使用 accumulate,但使累加器成为 std::string code> 您附加到的,或其他一些方法。

关于c++ - std::accumulate 有引用吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59932129/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com