没有匹配的C++操作符
我必须编写一个函数,将向量b附加到向量a a=1234 b=567,输出=1234567。我有这个问题,因为我有点新的东西,但我的问题是,我得到的错误没有匹配的运营商没有匹配的C++操作符,c++,vector,C++,Vector,我必须编写一个函数,将向量b附加到向量a a=1234 b=567,输出=1234567。我有这个问题,因为我有点新的东西,但我的问题是,我得到的错误没有匹配的运营商 由于运算符没有预定义的运算符,除了插入方法之外,还有其他方法吗?因为我还没有学过,我不想把我没有学过的东西包括进去class@darkphantom1插入方法有什么问题?我现在就写我的文章。我在课堂上还没有学过这个方法,所以我不想用它,除非没有其他方法,但我肯定有 #include <iostream> #includ
由于运算符没有预定义的运算符,除了插入方法之外,还有其他方法吗?因为我还没有学过,我不想把我没有学过的东西包括进去class@darkphantom1插入方法有什么问题?我现在就写我的文章。我在课堂上还没有学过这个方法,所以我不想用它,除非没有其他方法,但我肯定有
#include <iostream>
#include <vector>
using namespace std;
vector<int> merge(vector<int> a, vector<int> b)
{
int n = a.size();
int m = b.size();
vector<int> c;
int i;
for (i = 0; i < n || i < m; i++)
{
if (i < n)
{
c.push_back(a[i]);
}
if (i < m)
{
c.push_back(b[i]);
}
}
return c;
}
int main ()
{
vector<int> v1(4,1);
vector<int> v2(3,2);
cout << merge (v1,v2) <<endl;;
return 0;
}
cout << merge (v1,v2) <<endl;
template<typename T>
std::ostream& operator<<(std::ostream& stream, std::vector<T> const& xs) {
for (auto&& x : xs) {
stream << x << ", ";
}
return stream;
}
for ( int x : merge( a, b ) ) std::cout << x << ' ';
std::cout << std::endl;
std::vector<int> merge( const std::vector<int> &a, const std::vector<int> &b )
{
std::vector<int> c( a.begin(), a.end() );
c.insert( c.end(), b.begin(), b.end() );
return c;
}
#include <iostream>
#include <vector>
std::vector<int> merge( const std::vector<int> &a, const std::vector<int> &b )
{
std::vector<int> c( a.begin(), a.end() );
c.insert( c.end(), b.begin(), b.end() );
return c;
}
int main()
{
std::vector<int> v1( 4, 1 );
std::vector<int> v2( 3, 2 );
for ( int x : merge( v1, v2 ) ) std::cout << x << ' ';
std::cout << std::endl;
return 0;
}
1 1 1 1 2 2 2
std::vector<int> merge( const std::vector<int> &a, const std::vector<int> &b )
{
std::vector<int> c;
c.reserve( a.size() + b.size() );
for ( std::vector<int>::size_type i = 0; i < a.size(); i++ ) c.push_back( a[i] );
for ( std::vector<int>::size_type i = 0; i < b.size(); i++ ) c.push_back( b[i] );
return c;
}