C++ C++;带有GCC/codeblocks的stl映射编译器问题
下面的代码应该适用于VS2008,但我在第53行遇到了问题 其中: 我使用的是codeblocks/mingw/gcc4.xxC++ C++;带有GCC/codeblocks的stl映射编译器问题,c++,stl,map,C++,Stl,Map,下面的代码应该适用于VS2008,但我在第53行遇到了问题 其中: 我使用的是codeblocks/mingw/gcc4.xx #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
#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;
}
#包括
#包括
#包括
使用名称空间std;
//定义与newMap一起使用的联合_
联合大学
{
字符c;
int i;
}u;
//让我们定义两个不同的映射
//第一个参数是键,第二个参数是值
map-portMap;
地图新地图;
int main()
{
//portmap中的第一个条目
端口映射u[“第一”]=1;
//使用迭代器的示例
map::const_迭代器it;
字符串z=“秒”;
it=portMap_zy.find(z);//不在映射中,因此找不到
if(it==portMap_uz.end())
{
portMap_z]=22;//添加第二个元素
}
//直接添加第三个元素
z=“第三”;
portMap_z]=12345;
//通过插入添加第四个元素
端口图插入(成对(“第四”,4444));
//通过插入添加第5个元素
端口图插入(成对(“第五”,5555));
cout尝试从map::const\u iterator it;
更改为map::iterator it
尝试从map::const\u iterator it;
更改为map::iterator it
您的迭代器定义为const。尝试非常量迭代器
您也可以通过简单地使用key_类型来擦除元素。在您的示例中,它将是:
portMap_.erase(z);
您的迭代器定义为常量。请尝试非常量迭代器
您也可以通过简单地使用key_类型来擦除元素。在您的示例中,它将是:
portMap_.erase(z);
或者只调用portmap.erase(“第四”);
。或者只调用portmap.erase(“第四”);
。严格地说,迭代器不是“定义为常量”,而是一个非常量常量迭代器。另外,C++0x缓解了这个问题,并允许常量迭代器进行擦除。严格地说,迭代器不是“定义为常量”,但它是一个非常量常量迭代器。此外,C++0x缓解了此问题,并允许常量迭代器进行擦除,是的。如果我没有弄错,您的代码在C++0x中应该是正常的。只是当前库规范中的一个缺陷。如果我没有弄错,您的代码在C++0x中应该是正常的。只是当前库规范中的一个缺陷。
portMap_.erase(z);