0

嘿,我有一个结构

typedef struct CLUSTERINFO{
 unsigned cluster;
 vector <string> scopids;
 }clusterinfo;

看起来我有一些问题将值分配给向量 scopids 然后打印出来

multimap<unsigned, clusterinfo> classinfomap;
clusterinfo clinfo;
string id_req;
 //vector<unsigned> cluster_req_list and clustersipinfomap are some known from previous modules
for (ib=cluster_req_list.begin(); ib !=cluster_req_list.end(); ib++)
     {
      if(clustersipinfomap.count(*ib)>0)
   {        
        cout<<count1<<"\t"<<*ib<<"\t"; 
    clinfo.cluster= *ib;
    std::pair<multimap<unsigned,sipinfo>::iterator, multimap<unsigned,sipinfo>::iterator> ret;
    set<string>id_req_list;
    id_req_list.clear();
    ret=clustersipinfomap.equal_range(*ib);
    //obtain the id_req_list 
    for (multimap<unsigned, sipinfo>:: iterator ic=ret.first; ic!=ret.second; ++ic)
    {
         string id_tmp=ic->second.id;
        id_req_list.insert(id_tmp);
         *****(clinfo.scopids).push_back(id_tmp);   //i got sth wrong here


    }   

再次打印出结构中的向量是错误的;

 multimap<unsigned, clusterinfo>::iterator ip;
   for(ip= classinfomap.begin(); ip!=classinfomap.end(); ip ++)
   {
         cout<<ip->first <<"\t"<< ip->second.cluster<<endl;
        for (unsigned it=0; it< (ip->second.scopids).size(); it++)
        {
            count<< (ip->second.scopids)[it] << endl;
        }

   }
4

1 回答 1

0

如何为结构中的向量“分配”一个值:您可能希望向向量添加一个元素,您可以通过以下方式实现std::vector::push_back

struct Foo
{
  std::vector<std::string> vec;
};

Foo f;
std::string s("Hello, World!";
f.vec.push_back(s);

如何打印出向量的内容?

C++11

for (const auto& e : f.vec)
  std::cout << e << " ";
std::cout << std::endl;

C++03

for (std::vector<std::string>::const_iterator it = f.vec.begin(); it != f.vec.end(); ++it)
  std::cout << *it << " ";
std::cout << std::endl;    
于 2013-07-02T16:54:46.170 回答