请各位大侠指点啊,如何实现两个vector<string>类型的相加?
1个回答
展开全部
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
void print( const vector<string>& vec1,const vector<string>& vec2 )
{
copy( vec1.begin(),vec1.end(),ostream_iterator< string >( cout," " ));
cout<<endl;
copy( vec2.begin(),vec2.end(),ostream_iterator< string >( cout," " ));
cout<<endl;
}
int main()
{
vector<string> vec1;
vec1.push_back("hahhh");
vec1.push_back("dkkdk");
vector<string> vec2;
vec2.push_back("dkk");
vec2.push_back("djjdj");
print(vec1,vec2);
//方法1:
for( vector<string>::iterator it = vec2.begin();it!=vec2.end();++it )
vec1.push_back(*it);
print(vec1,vec2);
//方法2:
copy( vec2.begin(),vec2.end(),back_insert_iterator<vector<string> >( vec1 ) );
print(vec1,vec2);
return 0;
}
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
void print( const vector<string>& vec1,const vector<string>& vec2 )
{
copy( vec1.begin(),vec1.end(),ostream_iterator< string >( cout," " ));
cout<<endl;
copy( vec2.begin(),vec2.end(),ostream_iterator< string >( cout," " ));
cout<<endl;
}
int main()
{
vector<string> vec1;
vec1.push_back("hahhh");
vec1.push_back("dkkdk");
vector<string> vec2;
vec2.push_back("dkk");
vec2.push_back("djjdj");
print(vec1,vec2);
//方法1:
for( vector<string>::iterator it = vec2.begin();it!=vec2.end();++it )
vec1.push_back(*it);
print(vec1,vec2);
//方法2:
copy( vec2.begin(),vec2.end(),back_insert_iterator<vector<string> >( vec1 ) );
print(vec1,vec2);
return 0;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询