std :: map< int,std :: string [2]>问题

编程入门 行业动态 更新时间:2024-10-27 18:31:19
本文介绍了std :: map< int,std :: string [2]>问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

首先,请原谅我,如果这是一个错误的地方提出这个问题, 如果这是一个愚蠢的问题(这是我用C ++的第二周),或者如果这个是 回答了其他地方(我已搜索但未找到任何东西)。 这里有问题,我有两套文件,文件的名称包含一个 数字,对于每个集合都是唯一的,但是可能(甚至可能) 不同集合中的两个文件具有相同的数字。我希望以这样的方式存储这些 文件名,以便我可以通过它们的编号来检索它们。对于这个 我以为我会使用一个数字作为键的地图,一个字符串数组 来存储文件名,但是我已经尝试使用 此解决方案时遇到麻烦。 将文件名添加到地图时我不知道文件名是否来自 其他套装已经存在,但举例说明: www.fredosaurus/notes-cpp.../map/pair.html www.fredosaurus/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?m

First 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 is answered 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 a number which is unique for each set but it''s possible (even probable) that two files in different sets have the same numbers. I want to store these file-names in such a way that I can retrieve them by their number. For this I thought I''d use a map with the number as the key an an array of strings to store the file-names, however I''ve run into trouble when trying to use this solution. When adding the file-names to the map I don''t know if a filename from the other set already exists, but given examples at: www.fredosaurus/notes-cpp.../map/pair.html www.fredosaurus/notes-cpp...-wordfreq.html it seams like entries not found are created and if they are found I can modify 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 filename map<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 filenames file1 = &(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 set int fileNumber = getNumber((*files)[i]); (fileSets[fileNumber])[setNr] = (*files)[i]; // This does not work ^^^^^^^ } } // Set the iterator iter = 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 an array type `std::string[2]'' ------------------------------------------ and I get similar results when using gcc 3.4. Obviously there''s some problem with the array but I can''t figure out what and would appreciate a nudge in the right 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 known but if I have an (bidirectional) iterator pointing at some element in the map, and know that the element I''m interested in is N elements from the one the iterator is currently pointing at can I jump to that element by adding N to the iterator, e.g. iter+=N ? -- Erik Wikstr?m

推荐答案

Erik Wikstr?m写道: Erik Wikstr?m wrote: 首先,请原谅我这个问题是错误的, 不是。你很好。 如果这是一个愚蠢的问题(这是我用C ++的第二周),或者如果这是在其他地方回答的话(我已经搜索但没有找到任何东西)。 它可能已经被回答了,但有时它很难找到。 [...] map< int,string [2]>文件集; 你根本做不到。数组不能是标准 容器中的存储数据。如果你需要两个字符串,要么成对(std :: pair) 或制作你自己的结构。 [...] First of all, forgive me if this is the wrong place to ask this question, It''s not. You''re fine. if it''s a stupid question (it''s my second week with C++), or if this is answered some place else (I''ve searched but not found anything). It may have been answered already, but sometimes it''s rather hard to find. [...] map<int, string[2]> fileSets; You simply can''t do that. Arrays cannot be the stored data in a standard container. If you need two strings there, either make a pair (std::pair) or make your own struct. [...]

V

V

" Erik Wikstr?m" < ER *********** @ telia> schrieb im Newsbeitrag 新闻:XO ******************* @ newsb.telia ... "Erik Wikstr?m" <er***********@telia> schrieb im Newsbeitrag news:XO*******************@newsb.telia... 这就是问题所在,我有两组文件,一个文件的名称包含一个数字,这个数字对于每一组都是唯一的,但它可能(甚至可能)不同组中的两个文件具有相同的数字。我希望以这样的方式存储这些文件名,以便我可以通过它们的编号来检索它们。对于这个我以为我会用一个数字作为键的地图,一个字符串数组来存储文件名,但是我遇到了麻烦。试图使用这个解决方案。 正如Victor已经解释过的那样,你不能使用C风格的数组作为标准容器的值 类型。但是你可以使用std :: vector。 但是,当你描述你的问题时,你有两组文件,而不是一组文件对。所以我建议使用两个 std :: map< int,std :: string>的实例,每组文件一个。 PS:如果密钥未知,则无法随机访问地图但如果我有一个(双向)迭代器指向地图中的某个元素,并且知道该元素我感兴趣的是来自的N个元素。迭代器当前指向的是我可以通过向迭代器添加 N来跳转到该元素,例如iter + = N? Here''s the problem, I have two sets of files, the name of a file contains a number which is unique for each set but it''s possible (even probable) that two files in different sets have the same numbers. I want to store these file-names in such a way that I can retrieve them by their number. For this I thought I''d use a map with the number as the key an an array of strings to store the file-names, however I''ve run into trouble when trying to use this solution. As Victor already explained, you cannot use a C style array as the value type of a standard container. But you could use an std:: vector instead. However, as you describe your problem, you have two sets of files, not one set of pairs of files. So I would suggest to use two instances of std::map<int, std::string>, one for each set of files. PS: It''s not possible to get random access to a map if the key is not known but if I have an (bidirectional) iterator pointing at some element in the map, and know that the element I''m interested in is N elements from the one the iterator is currently pointing at can I jump to that element by adding N to the iterator, e.g. iter+=N ?

看看std :: advance。 HTH Heinz

Have a look at std::advance. HTH Heinz

> map< int,string [2]> fileSets; string [2]没有=运算符且没有<运营商 - 这就是整个 问题。 -Gernot > map<int, string[2]> fileSets; string[2] has no = operator and no < operator - that''s the whole problem. -Gernot

更多推荐

std :: map&lt; int,std :: string [2]&gt;问题

本文发布于:2023-07-15 18:11:19,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1112867.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:amp   map   std   lt   gt

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!