本文介绍了std :: map&lt; int,std :: string [2]&gt;问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 首先,请原谅我,如果这是一个错误的地方提出这个问题, 如果这是一个愚蠢的问题(这是我用C ++的第二周),或者如果这个是 回答了其他地方(我已搜索但未找到任何东西)。 这里有问题,我有两套文件,文件的名称包含一个 数字,对于每个集合都是唯一的,但是可能(甚至可能) 不同集合中的两个文件具有相同的数字。我希望以这样的方式存储这些 文件名,以便我可以通过它们的编号来检索它们。对于这个 我以为我会使用一个数字作为键的地图,一个字符串数组 来存储文件名,但是我已经尝试使用 此解决方案时遇到麻烦。 将文件名添加到地图时我不知道文件名是否来自 其他套装已经存在,但举例说明: http://www.fredosaurus.com/notes-cpp.../map/pair.html http://www.fredosaurus.com/notes-cpp。 ..- wordfreq.html 它创建了未找到的条目,如果找到它们我可以 修改它们。 这里的代码显示我的问题: #include< map> #include< string> #include< vector> 使用std :: map; 使用std :: string; 使用std :: vector; class Files { public: void nextFile(string *,string *); void open(vector< string> *,int); private: int getNumber(string); //从文件名中获取数字 map< int,string [2]> fileSets; map< int,string [2]> :: iterator iter; }; //返回下一对文件 //我已经删除了一些代码以保持简短, //除其他外检查fileSets.end()。 void Files :: nextFile(string * file1,string * file2) { iter ++; / /返回指向文件名的指针 file1 =&(iter-> second [0]); file2 =&(iter-> second [1 ]); } //将文件添加到集合 //文件是一个带有要添加的文件名的向量。 /> // setNr是文件应该添加的集合的数量,可以是0或1. void Files :: open(vector< string> * files,int setNr) { if(setNr< 2) { for(int i = 0; i< files-> size(); i ++) { //将文件添加到正确的集合 int fileNumber = getNumber((* files)[i]); (fileSets [fileNumber])[setNr] =(* files)[i]; //这不起作用^^^^^^^ } } //设置迭代器 iter = fileSets.begin(); } ----------------------------------- ------- 在gcc 3.3上编译时,我得到以下内容: /usr/include/c++/3.3。 4 / bits / stl_map.h:在成员函数`_Tp& std :: map< _Key, _Tp,_Compare,_Alloc> :: operator [](const _Key&)[with _Key = int,_Tp = std :: string [2],_ Compare = std :: less< int>,_ Alloc = std :: allocator< std :: pair< const int,std :: string [2]> >]'': Files.cpp:46:从这里实例化 /usr/include/c++/3.3.4/bits/stl_map.h:319:错误:ISO C ++禁止转换为 数组类型`std :: string [2]'' ---------- -------------------------------- 我在使用gcc 3.4时得到了类似的结果。很显然,阵列存在一些问题 ,但是我无法弄清楚是什么,并且会欣赏 正确的方向,并且因为我'''我还在学习任何其他评论。 PS: 如果密钥未知,则无法随机访问地图 但如果我有一个(双向)迭代器指向 映射中的某个元素,并且知道我感兴趣的元素是来自一个元素的N个元素br /> 迭代器当前指向我可以通过向迭代器添加 N来跳转到该元素,例如iter + = N? - Erik Wikstr?mFirst of all, forgive me if this is the wrong place to ask this question,if it''s a stupid question (it''s my second week with C++), or if this isanswered some place else (I''ve searched but not found anything).Here''s the problem, I have two sets of files, the name of a file contains anumber which is unique for each set but it''s possible (even probable) thattwo files in different sets have the same numbers. I want to store thesefile-names in such a way that I can retrieve them by their number. For thisI thought I''d use a map with the number as the key an an array of stringsto store the file-names, however I''ve run into trouble when trying to usethis solution.When adding the file-names to the map I don''t know if a filename from theother set already exists, but given examples at: http://www.fredosaurus.com/notes-cpp.../map/pair.html http://www.fredosaurus.com/notes-cpp...-wordfreq.htmlit seams like entries not found are created and if they are found I canmodify them.Here''s code that displays my problem:#include <map>#include <string>#include <vector>using std::map;using std::string;using std::vector;class Files{public:void nextFile(string*, string*);void open(vector<string>*, int);private:int getNumber(string); // Gets the number from the filenamemap<int, string[2]> fileSets;map<int, string[2]>::iterator iter;};// Return the next pair of files// I''ve removed a bit of code to keep it short,// among other things checks for fileSets.end().void Files::nextFile(string* file1, string* file2){iter++;// Return the pointers to the filenamesfile1 = &(iter->second[0]);file2 = &(iter->second[1]);}// Add files to the sets// files is a vector with the filenames to be added.// setNr is the number of the set the files should be// added to, can be 0 or 1.void Files::open(vector<string>* files, int setNr){if(setNr < 2){for(int i = 0; i < files->size(); i++){// Add the file to the correct setint fileNumber = getNumber((*files)[i]);(fileSets[fileNumber])[setNr] = (*files)[i];// This does not work ^^^^^^^}}// Set the iteratoriter = fileSets.begin();}------------------------------------------When compiling on gcc 3.3 I get the following:/usr/include/c++/3.3.4/bits/stl_map.h: In member function `_Tp& std::map<_Key,_Tp, _Compare, _Alloc>::operator[](const _Key&) [with _Key = int, _Tp =std::string[2], _Compare = std::less<int>, _Alloc =std::allocator<std::pair<const int, std::string[2]> >]'':Files.cpp:46: instantiated from here/usr/include/c++/3.3.4/bits/stl_map.h:319: error: ISO C++ forbids casting to anarray type `std::string[2]''------------------------------------------and I get similar results when using gcc 3.4. Obviously there''s some problemwith the array but I can''t figure out what and would appreciate a nudge in theright direction, and since I''m still learning any other comment too.PS:It''s not possible to get random access to a map if the key is not knownbut if I have an (bidirectional) iterator pointing at some element in themap, and know that the element I''m interested in is N elements from the onethe iterator is currently pointing at can I jump to that element by addingN to the iterator, e.g. iter+=N ?--Erik Wikstr?m推荐答案 VV 看看std :: advance。 HTH HeinzHave a look at std::advance.HTHHeinz 这篇关于std :: map&lt; int,std :: string [2]&gt;问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
09-03 09:56