[英]Error: no match for 'operator+' (operand types are 'std::__cxx11::list<int>' and 'int')|
I'm trying to take an even integer array and odd integer list, and then try to merge them in a vector, using the merge algorithm available in STL of C++. I'm trying to take an even integer array and odd integer list, and then try to merge them in a vector, using the merge algorithm available in STL of C++. The task requires specifically to use the merge algorithm, and I'm getting stuck in error at line 43 , right of which I've commented that that's the error point.
该任务需要专门使用合并算法,我在第 43 行遇到错误,我已经评论说这是错误点。
#include <iostream>
#include <algorithm>
#include <functional>
#include <cstring>
#include <vector>
#include <list>
#include <cstdlib>
using namespace std;
void display(list<int>&l){
list<int>::iterator p;
for(p=l.begin();p!=l.end();++p){
cout<<*p<<", ";
}
cout<<"\n";
}
void display(int arr[6]){
for(int i=0;i<6;i++){
cout<<arr[i]<<" ";
}
cout<<"\n";
}
int main()
{
int inp;
int even_arr[6];
list<int> odd_list(6);
cout<<"Enter even numbers: ";
for(int i=0;i<6;i++){
cin>>even_arr[i];
}
display(even_arr);
list<int>::iterator p;
cout<<"\nEnter odd numbers: ";
for(p=odd_list.begin();p!=odd_list.end();++p){
cin>>inp;
*p = inp;
}
display(odd_list);
vector<int> vec1(12);
list<int>::iterator itr=odd_list.begin();
int *ptr=even_arr;
merge(even_arr,even_arr+6,odd_list,odd_list+6,vec1); //ERROR LINE
return 0;
}
You can't add something to a std::list
like you can do with arrays.您不能像使用 arrays 那样向
std::list
添加内容。 Also it's better to be explicit about iterators and use std::end
to get the past-the-end-iterator, since you want to use the whole array anyway.此外,最好明确迭代器并使用
std::end
来获取过去的迭代器,因为无论如何您都想使用整个数组。
Also std::merge()
expects iterators but the last parameter is vec1
, which is a std::vector
. std::merge()
也需要迭代器,但最后一个参数是vec1
,它是一个std::vector
。 The correct version would be:正确的版本是:
merge(begin(even_arr), end(even_arr),
begin(odd_list), end(odd_list),
begin(vec1));
I omitted std::
to improve readability.我省略了
std::
以提高可读性。
Use of std::begin
over odd_list.begin()
(and similar) is debatable and up to personal preference.在
odd_list.begin()
(和类似的)上使用std::begin
是有争议的,取决于个人喜好。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.