English 中文(简体)
2. 改变编中的价值:编造者地图
原标题:Change the value in std::map from iterator
  • 时间:2011-08-24 13:10:18
  •  标签:
  • c++
  • stdmap

我的申请合并了两个<代码>std:map例。 如果没有重复,合并就无干预。 然而,如果发现重复,那么这种方法就问,新价值是否应当被忽视或超写。 (可通过向用户发送规则表、电文箱或某种其他逻辑来回答这一问题......这只是一种从一个具有<代码>bool确认() 符合方法的纯虚拟类别衍生的类别的例子。)

If the insert fails, and they decide to overwrite the existing entry, I already have an iterator pointing to the correct item to update. Can I use this iterator to update the value directly, or do I have to call operator[] and take the hit of another lookup?

// typedef std::map<Foo, Foo, Compare> Dictionary;
// Dictionary this_dictionary, other_dictionary;
for (Dictionary::const_iterator i = other_dictionary.begin();
     i != other_dictionary.end();
     ++i) {
  std::pair<Dictionary::iterator,bool> ret = this_dictionary.insert(*i);
  if (!ret.second && confirmer.confirm()) {
    // ???
  }
}
最佳回答

您需要使用<代码>Dictionary:iterator,而不是Dictionary:const_iterator<<>code>, 返回。

for (Dictionary::const_iterator i = other_dictionary.begin();
     i != other_dictionary.end();
     ++i) {
  // Use standard iterator here
  std::pair<Dictionary::iterator,bool> ret = this_dictionary.insert(*i);
  if (!ret.second && confirmer.confirm()) {
    ret.first->second = i->first; 
  }
}
问题回答

你们可以,但你们需要使用它,而不是召集人。





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?