[英]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::vectorstd::vector<std::__cxx11::basic_string<char >>::push_back(std::__cxx11::basic_string&)' 結果.push_back(*it1);
此代碼的目的是將表復制到結果上。 什么是正確的解決方案? 換句話說,您如何將一個向量向量復制到另一個向量向量上?
哎呀,是的太多層(我應該理解第一條評論) - 這是所需的代碼 -
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);
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.