【问题标题】:Map iterator issues. Code included映射迭代器问题。包含代码
【发布时间】:2013-08-01 00:07:19
【问题描述】:

所以这是我所有的代码,我真的不明白为什么会出现这些错误。 问题出在导出配方功能中。

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


using namespace std;

void DisplayMenu();
void AddRecipe( map< string, vector<string> >& recipes );
void ExportRecipes( map< string, vector<string> >& recipes );

int main ( void ){

   int choice = 0;
   bool done = false;
   map< string, vector<string> > recipes;

    while ( done == false ){
       DisplayMenu();
       cin >> choice;

       if ( choice == 3 ){
        done = true;
       }
       else if ( choice == 2 ){
        ExportRecipes( recipes );
       }
       else if ( choice == 1 ){
        AddRecipe( recipes );
       }

    }
}

void DisplayMenu(){ 
   cout << "1. Add Recipe " << endl;
   cout << "2. Export Recipes " << endl;
   cout << "3. Exit" << endl;
}

void AddRecipe( map< string, vector<string> >& recipes ){

   string name, ingredient;
   bool done = false;
   cout << "Enter recipe name: ";
   cin >> name;
   while ( done == false ){     
       cout  << "Enter new ingredient and amount( enter done to exit )" << endl;
       getline( cin , ingredient, '\n' );

       if ( ingredient == "done" ){
           done = true;
       }

       if( ingredient != "done"){
           recipes[ name ].push_back( ingredient );
           cout << "Added \"" << ingredient << "\"." << endl << endl;
       }        
   }
}


void ExportRecipes(  map< string, vector<string> >&recipes ){

   ofstream outFile;
   outFile.open( "Recipes.txt" );

   for ( map< string, vector<string> >::iterator recipe =
       recipes.begin(); recipe != recipes.end(); recipe++ ) {
       outFile << endl << endl << recipe -> first << endl;

       for ( map< string, vector<string> >::iterator ingredients = 
           recipe->second.begin(); ingredients != recipe->second.end();
            ingredients++ ) {
               outFile << "\t" << *ingredients << endl;
       }
   }
}

如果我只遍历导出中的第一个 for 循环,我可以获得密钥,但我根本无法获得值。

【问题讨论】:

  • 什么是“问题”?我不确定你最后一句是什么意思。
  • 我无法访问成分迭代器中的数据。
  • 你试过我的建议了吗?
  • 错误 1 ​​错误 C2440:'正在初始化':无法从 'std::_Vector_iterator<_myvec>' 转换为 'std::_Tree_iterator<_mytree>' c:\users\jason\documents\visual studio 2012\projects\assignment 5\assignment 5\source.cpp 89
  • 太棒了,我把它打开了,它现在可以工作了!

标签: c++ map iterator


【解决方案1】:

为什么你有第二个带有 Map 的 for 循环。 recipe->second 是一个向量,所以试试这个 -

void ExportRecipes(  map< string, vector<string> >&recipes ){
   ofstream outFile;
   outFile.open( "Recipes.txt" );

   for ( map< string, vector<string> >::iterator recipe =
       recipes.begin();
        recipe != recipes.end();
        recipe++ )
   {
      outFile << endl << endl << recipe -> first << endl;

      for ( vector<string>::iterator ingredients = 
        recipe->second.begin();
            ingredients != recipe->second.end();
            ingredients++ )
        {
            outFile << "\t" << *ingredients << endl;
        }

   }
 }

【讨论】:

    【解决方案2】:
    for ( map< string, vector<string> >::iterator ingredients = 
            recipe->second.begin();
    

    recipe-&gt;secondvector&lt;string&gt;。因此,recipe-&gt;second.begin() 返回vector&lt;string&gt;::iterator,而不是map&lt; string, vector&lt;string&gt; &gt;::iterator

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-05-01
      • 2021-04-15
      • 2011-07-31
      • 2021-05-24
      • 2021-07-06
      • 2017-12-22
      • 2011-04-02
      相关资源
      最近更新 更多