【问题标题】:comparison between mapvalue and iterator c++mapvalue和迭代器c ++之间的比较
【发布时间】:2015-03-11 09:40:07
【问题描述】:

我想知道是否有办法比较两个值以查看它们是否相等。我有一个映射,其中键是 int,类型是类。我想找到一个特定的数字,看看它是否存在于地图中,如果它做了某个动作应该做。

即:我有一个存储在我的地图容器中的银行帐户。我想更改该帐户的余额值。

我的地图声明中的 int 是帐号。

map<int, class>::iterator it;
map<int, class>::myMap;



void Deposit(){
int banknbr;
int amount;
cout << "What is your acc numb?" << endl;
cin >> bankNbr;
for(it = myMap.begin(); it != myMap.end(); it++){
   if(it->first==myMap[bankNbr]){
     cout << "How much money do you wish to deposit?" << endl;
     cin >> amount;
     deposit(); // balance = balance + amount;

或者类似的东西。如果我存储两个帐户,我会覆盖第一个帐户的余额。所以如果第一个账户的余额是 100,第二个是 200,我想在第一个账户存入 100 钱,我的值变成了 300,这是不正确的。

我现在的代码是这样的:

void deposit(int number){
int amount;
for(it = myMap.begin(); it != myMap.end(); it++){
    myMap.find(number);
    if(it != myMap.end(){
        cout << "How much money do you wish to deposit?"<<endl;
        cin >> amount;
            acc.deposit(amount); //acc is my own implemented class
            cout << "Your balance is: " << acc.getBal(number) << endl; //get bal is return balance
}
            }



}

【问题讨论】:

  • 你检查过docs吗?
  • en.cppreference.com/w/cpp/container/map/find 表明 find 如果找到则将迭代器返回到找到的条目,否则返回 end()。所以你想要if (myMap.find(number) != myMap.end()) {}
  • 您添加的代码没有找到任何东西。它只是遍历整个地图。您应该使用 myMap.find() 函数的结果,而不是使用 for 循环。
  • 您正在将 for 循环运行到地图 myMap 的大小。 if 条件为真,因为it 不等于map.end 并添加了金额。所以金额被添加sizeOfMap - 1 次。并且声明myMap.find 无效。您可以删除此语句并将获得相同的结果。检查我在答案中所做的编辑。
  • 我已经检查了文档。好的,感谢您提供的新知识,如果我使用 forloop,不知道 find() 没有任何效果。我会试试你的编辑。

标签: c++ dictionary iterator comparison containers


【解决方案1】:

您可以使用mapfind方法来查找密钥是否存在。

如果键存在,find 方法返回一个迭代器,否则 map::end

it=mymap.find(number);
if(it!=myMap.end()){
   //your code 
}

你可以在这里找到更多关于这个方法的细节http://www.cplusplus.com/reference/map/map/find/

 //for(it = myMap.begin(); it != myMap.end(); it++){
it = myMap.find(number);   //assign "it" 
if(it != myMap.end(){
    cout << "How much money do you wish to deposit?"<<endl;
    cin >> amount;
        acc.deposit(amount); //acc is my own implemented class
        cout << "Your balance is: " << acc.getBal(number) << endl; //get bal is return    balance
  }
       // }

【讨论】:

  • key_comp 返回一个比较对象。在这里好像没什么用。
  • @interjay 是的,你是对的。我已经编辑了我的答案
  • 感谢您的建议,它部分工作。使用 acc.getBal() 函数时,天平上仍然出现更新错误。我需要 it->second.getBal() 以使其正常工作。
【解决方案2】:

这是你的问题:

for(it = myMap.begin(); it != myMap.end(); it++){ // it will iterate through all acounts
    myMap.find(number); // find returns an iterator BUT YOU DON'T ASSIGN IT
    if(it != myMap.end(){ // For each iteration this will be true, cause it is only changed in the for statement

find 是处理这个问题的正确方法:

void Deposit(){
    int banknbr;

    cout << "What is your acc numb?" << endl;
    cin >> bankNbr;

    auto it = myMap.find(bankNbr);

    if(it != myMap.end()){
        int amount;

        cout << "How much money do you wish to deposit?" << endl;
        cin >> amount;
        it->second.deposit(amount);
        cout << "Your balance is: " << it->second.getBal() << endl; //get bal is return balance
    }
}

您似乎对 map 的工作方式有一些误解,所以如果不清楚,请告诉我。

【讨论】:

  • 谢谢,有了这个解释,地图对我来说更清楚了。它使我的代码正常工作。因此,我应该使用映射的值来调用函数,而不是使用创建的类对象。
  • @SamGoat 如果我理解正确,是的。您的map 应使用帐号作为键,使用帐户作为值。所以你可以通过账号搜索map,然后对映射到的账号进行操作。
猜你喜欢
  • 2017-05-02
  • 2012-11-05
  • 2016-07-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-10-05
  • 1970-01-01
相关资源
最近更新 更多