简体   繁体   English

从向量对的向量向量插入向量串的向量<int,string>

[英]inserting in a vector of vector string from vector vector of vector pair<int,string>

vector<vector<pair<int,string>>> v(mxfreq+1);
  for(it=m.begin();it!=m.end();it++)
  {
   v[it->second.second].push_back(make_pair(it->second.first,it->first));

  }
  vector<vector<string>> v1;
  for(int i=mxfreq;i>=0;i--)
  {
    string t=to_string(i);
    if(v[i].size()>1)
    {
      sort(v.begin(),v.end());
    }
    for(int j=0;j<v[i].size();j++)
    {
      v1.push_back(v[i][j].second);
      v1.push_back(t);
    }
  }
//cout<<v[0][0].first<<endl;
  return v1; 

here i am trying to insert it into a vector of vector string from vector of vector pair<int,string> but i am getting error v1.push_back(v[i][j].second);在这里,我试图将它从向量 pair<int,string> 的向量插入到向量字符串的向量中,但我收到错误v1.push_back(v[i][j].second); here the mxfreq we can assume to be a integer >0这里我们可以假设mxfreq是一个大于 0 的整数

To implement the "simple" solution using range-based for loops nested inside each other, it could be done something like:要使用相互嵌套的基于范围的for循环来实现“简单”解决方案,可以执行以下操作:

// Make sure the destination vector have enough memory allocated
v1.reserve(v.size());

for (auto const& v_pairs : v)
{
    // Create a temporary vector for the string
    std::vector<std::string> v_strings;

    // And make sure it have enough memory allocated
    v_strings.reserve(v_pairs.size());

    // Copy the string from each pair into the string vector
    for (auto const& pair : v_pairs)
    {
        v_strings.emplace_back(pair.second);
    }

    // And move the string vector into the destination vector
    v1.emplace_back(std::move(v_strings));
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM