首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用GCC/代码块的C++ stl映射编译器问题

使用GCC/代码块的C++ stl映射编译器问题
EN

Stack Overflow用户
提问于 2011-07-22 01:00:36
回答 2查看 2.2K关注 0票数 1

下面的代码应该可以与VS2008一起工作,但我在第53行遇到了问题:

代码语言:javascript
复制
portMap_.erase(it);

我正在使用代码块/mingw/gcc 4.xx

代码语言:javascript
复制
#include<iostream>
#include<map>
#include<string>

using namespace std;

//defining a union that is used with newMap_
union uu
{
  char c;
  int i;
} u;

//Lets define two different maps
//The first parameter is key and second value
map<string, int> portMap_;
map<void *, uu> newMap_;

int main()
{
  //first entry in portmap
  portMap_["first"] = 1;

  //example of using the iterator
  map<string, int>::const_iterator it;
  string z = "second";
  it = portMap_.find(z); //not in the map so wont be found
  if(it == portMap_.end())
  {
    portMap_[z] = 22; //add second element
  }

  //Add thrid element directly
  z = "third";
  portMap_[z] = 12345;

  //Add 4th element by insert
  portMap_.insert(pair<string,int>("fourth", 4444));

  //Add 5th element by insert
  portMap_.insert(pair<string,int>("fifth", 5555));


  cout<<"\n** Printing the portmap_ values **"<<endl;
  for(it = portMap_.begin(); it != portMap_.end(); ++it)
    cout<<"Key = "<<it->first<<"   Val = "<<it->second<<endl;

  cout<<"\n** Removing fourth element **"<<endl;
  z = "fourth";
  it = portMap_.find(z);
  portMap_.erase(it);

  cout<<"\n** Printing the portmap_ values **"<<endl;
  for(it = portMap_.begin(); it != portMap_.end(); ++it)
    cout<<"Key = "<<it->first<<"   Val = "<<it->second<<endl;

  //Playing with New Map
  cout<<"\n\nCreating New Map whose key is a void pointer"<<endl;

  uu u_val1, u_val2;
  void *val1, *val2;
  u_val1.i = 70, val1 = &u_val1;
  newMap_[val1]=u_val1;

  val2 = val1;
  map<void *, uu>::const_iterator it_new;
  it_new = newMap_.find(val2);
  if(it_new != newMap_.end())
  {
    u_val2 = it_new->second;
    cout<<"Note that since u_val2 is a union you can print i or c as required"<<endl;
    cout<<"val2 = "<<val2<<"    value.c = "<<u_val2.c<<endl;
    cout<<"val2 = "<<val2<<"    value.i = "<<u_val2.i<<endl;
  }

  return 0;
}

下面是错误:

代码语言:javascript
复制
map_example\map_example.cpp||In function 'int main()':|
map_example\map_example.cpp|51|error: no matching function for call to 'std::map<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, int, std::less<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, int> > >::erase(std::_Rb_tree_const_iterator<std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, int> >&)'|
c:\program files\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.4.1\include\c++\bits\stl_map.h|566|note: candidates are: void std::map<_Key, _Tp, _Compare, _Alloc>::erase(typename std::_Rb_tree<_Key, std::pair<const _Key, _Tp>, std::_Select1st<std::pair<const _Key, _Tp> >, _Compare, typename _Alloc::rebind<std::pair<const _Key, _Tp> >::other>::iterator) [with _Key = std::basic_string<char, std::char_traits<char>, std::allocator<char> >, _Tp = int, _Compare = std::less<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, _Alloc = std::allocator<std::pair<const std::basic_string<char,|
c:\program files\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.4.1\include\c++\bits\stl_map.h|581|note:                 typename std::_Rb_tree<_Key, std::pair<const _Key, _Tp>, std::_Select1st<std::pair<const _Key, _Tp> >, _Compare, typename _Alloc::rebind<std::pair<const _Key, _Tp> >::other>::size_type std::map<_Key, _Tp, _Compare, _Alloc>::erase(const _Key&) [with _Key = std::basic_string<char, std::char_traits<char>, std::allocator<char> >, _Tp = int, _Compare = std::less<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, _Alloc = std::allocator<std::pair<const std::basic_stri|
c:\program files\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.4.1\include\c++\bits\stl_map.h|596|note:                 void std::map<_Key, _Tp, _Compare, _Alloc>::erase(typename std::_Rb_tree<_Key, std::pair<const _Key, _Tp>, std::_Select1st<std::pair<const _Key, _Tp> >, _Compare, typename _Alloc::rebind<std::pair<const _Key, _Tp> >::other>::iterator, typename std::_Rb_tree<_Key, std::pair<const _Key, _Tp>, std::_Select1st<std::pair<const _Key, _Tp> >, _Compare, typename _Alloc::rebind<std::pair<const _Key, _Tp> >::other>::iterator) [with _Key = std::basic_string<char, std::char_traits<char>, std::all|
||=== Build finished: 1 errors, 0 warnings ===|
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-07-22 01:06:34

尝试从map<string, int>::const_iterator it;更改为map<string, int>::iterator it

票数 2
EN

Stack Overflow用户

发布于 2011-07-22 01:08:53

您的迭代器被定义为const。尝试非常量迭代器。

您还可以通过简单地使用key_type来删除元素。在您的示例中,它将是:

代码语言:javascript
复制
portMap_.erase(z);
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6779874

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档