如何使用迭代器将向量推回到向量的向量上
How to push back a vector onto a vector of vectors using an iterator
我的这段代码会产生错误:
#include <vector>
#include <iostream>
#include <string>
void read_string(std::string &str,
std::vector<std::string> &dir,
std::vector<std::vector<std::string> > &table,
std::vector<std::vector<std::string> > &result)
{
std::vector<std::vector<std::string> >::iterator it0;
std::vector<std::string>::iterator it1;
/*intent, iterate over each element (vector of strings) of the
vector of elements */
for(it0 = table.begin(); it0 != table.end(); it0++){
/*code to select specific vector of strings -added back to show intent*/
if((*it0)[0]==str){
/*selected vector of strings(element) are added to new table
called "result" */
for(it1 = (*it0).begin(); it1 != (*it0).end(); it1++){
result.push_back(*it1);
}
}
}
}
test.cc:18:28: error: no matching function for call to ‘std::vector<std::vector<std::__cxx11::basic_string<char> > >::push_back(std::__cxx11::basic_string<char>&)’
result.push_back(*it1);
此代码的目的是将 table
复制到 result
上。什么是正确的解决方案?换句话说,如何将一个向量的向量复制到另一个向量的向量上?
哎呀,层太多了(我应该理解第一条评论)-这是所需的代码-
void read_string(std::string &str,
std::vector<std::string> &dir,
std::vector<std::vector<std::string> > &table,
std::vector<std::vector<std::string> > &result)
{
std::vector<std::vector<std::string> >::iterator it0;
std::vector<std::string>::iterator it1;
//std::copy_if(table, table.size(), [](std::string p_str) {return
for(it0 = table.begin(); it0 != table.end(); it0++){
if((*it0)[0]==str){
result.push_back(*it0);
}
}
}
我的这段代码会产生错误:
#include <vector>
#include <iostream>
#include <string>
void read_string(std::string &str,
std::vector<std::string> &dir,
std::vector<std::vector<std::string> > &table,
std::vector<std::vector<std::string> > &result)
{
std::vector<std::vector<std::string> >::iterator it0;
std::vector<std::string>::iterator it1;
/*intent, iterate over each element (vector of strings) of the
vector of elements */
for(it0 = table.begin(); it0 != table.end(); it0++){
/*code to select specific vector of strings -added back to show intent*/
if((*it0)[0]==str){
/*selected vector of strings(element) are added to new table
called "result" */
for(it1 = (*it0).begin(); it1 != (*it0).end(); it1++){
result.push_back(*it1);
}
}
}
}
test.cc:18:28: error: no matching function for call to ‘std::vector<std::vector<std::__cxx11::basic_string<char> > >::push_back(std::__cxx11::basic_string<char>&)’
result.push_back(*it1);
此代码的目的是将 table
复制到 result
上。什么是正确的解决方案?换句话说,如何将一个向量的向量复制到另一个向量的向量上?
哎呀,层太多了(我应该理解第一条评论)-这是所需的代码-
void read_string(std::string &str,
std::vector<std::string> &dir,
std::vector<std::vector<std::string> > &table,
std::vector<std::vector<std::string> > &result)
{
std::vector<std::vector<std::string> >::iterator it0;
std::vector<std::string>::iterator it1;
//std::copy_if(table, table.size(), [](std::string p_str) {return
for(it0 = table.begin(); it0 != table.end(); it0++){
if((*it0)[0]==str){
result.push_back(*it0);
}
}
}