加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c – 如何从排序的数据创建向量的映射

发布时间:2020-12-16 03:00:03 所属栏目:百科 来源:网络整理
导读:我有以下数据作为输入(按第一列排序): foo 1 2foo 3 3bar 10 11 我想创建一个矢量地图,第一列是地图的关键 我们有: foo = {1,2,3,3}bar = {10,11} 但是为什么我的下面的代码不能按预期工作? #include vector #include map #include iostream #include fst
我有以下数据作为输入(按第一列排序):
foo 1 2
foo 3 3
bar 10 11

我想创建一个矢量地图,第一列是地图的关键
我们有:

foo = {1,2,3,3}
bar = {10,11}

但是为什么我的下面的代码不能按预期工作?

#include <vector>
    #include <map>
    #include <iostream>
    #include <fstream>
    #include <sstream>

    using namespace std;

    int main  ( int arg_count,char *arg_vec[] ) {
        if (arg_count !=2 ) {
            cerr << "expected one argument" << endl;
            return EXIT_FAILURE;      
        }

        string line;         
        ifstream acemblyfile (arg_vec[1]); 

        map <string,vector<int> > myMapOfVec; 
        vector <string> myVec;  
        string KEY = "" ;    


        if (acemblyfile.is_open())
        {
            while (getline(acemblyfile,line) )
            {
                stringstream ss(line);    
                string KEY_TEMP;
                int VAL1;
                int VAL2;           

                ss >> KEY_TEMP >> VAL1 >> VAL2;

                MyVec.push_back(VAL1);   
                MyVec.push_back(VAL2);   


                if (KEY_TEMP != KEY) {     
                  myMapOfVec[KEY] = MyVec;
                  KEY = KEY_TEMP;            
                  MyVec.clear();          
                }

            }
            acemblyfile.close();      
        }
        else {
            cout << "Unable to open file"; 
        }


for( map<string,vector<int> >::iterator iter = myMapOfVec.begin(); iter != myMapOfVec.end(); ++iter ) {
       vector <int> tempVec = (*iter).second;
       string Key = (*iter).first;
       for (unsigned i =0; i<tempVec.size(); i++) {
          cout << Key << " " << tempVec[i] << endl;
       }
    }

        return 0;
    }

解决方法

如Mykola所说,您应该使用地图中的向量,而不是自己创建一个.我改变了你的整个代码,所以它适用于我.注意,你写了一些错误的变量名称(MyMapOfVec而不是myMapOfVec),这导致编译器错误.

还要确保您的输入文件末尾没有换行符,因为这将导致重复最后一行.

#include <vector>
#include <map>
#include <iostream>
#include <fstream>
#include <sstream>

using namespace std;

int main  ( int arg_count,char *arg_vec[] ) {
    if (arg_count !=2 ) {
        cerr << "expected one argument" << endl;
        return EXIT_FAILURE;      
    }

    string line;         
    ifstream acemblyfile (arg_vec[1]); 

    map <string,vector<int> > myMapOfVec; 
    string KEY;    


    if (acemblyfile.is_open())
    {
        while (getline(acemblyfile,line) )
        {
            stringstream ss(line);    
            int VAL1;
            int VAL2;

            ss >> KEY >> VAL1 >> VAL2;

            myMapOfVec[KEY].push_back(VAL1);
            myMapOfVec[KEY].push_back(VAL2);

        }
        acemblyfile.close();      
    }
    else {
        cout << "Unable to open file"; 
    }


for( map<string,vector<int> >::iterator iter = myMapOfVec.begin(); iter != myMapOfVec.end(); ++iter ) {
   vector<int> tempVec = (*iter).second;
   string Key = (*iter).first;
   cout << Key;
   for (unsigned i = 0; i < tempVec.size(); i++) {
      cout << " " << tempVec[i];
   }
   cout << endl;
}

    return 0;
}

例如,这给出了输出

bar 10 11
foo 1 2 3 3

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读