我把一个值存到map里,可是取出来的时候是乱码,具体代码如下:
map<long,string>::iterator it1 = m_contCallidUcid.find(id);
if (it1 != m_contCallidUcid.end())
{
      value = it1->second;
}
打出来value的值发现时乱码,我调试到程序里,发现map里是有值得,单it1里的值和map中的值不一样,it1中的第一个值不是map的key,而且第二个值显示错误的指针,map里的值是正常的,我想问问怎么样才能取到正常的值。

解决方案 »

  1.   

    楼主你的map用法好奇怪啊为什么不这么用呢?
    typedef map<long,string> mymapmymap[1] = "123213";
    mymap[id]直接就是second的值啊
      

  2.   


    #include <iostream>
    #include <map>
    #include <string>
    using namespace std;
    int main(int argc, char* argv[])
    {
    map<long, string> MAP;
    MAP.insert(make_pair<long, string>(1, "AA"));
    MAP.insert(make_pair<long, string>(2, "BB"));
    MAP.insert(make_pair<long, string>(3, "CC"));
    map<long, string>::iterator it = MAP.find(2);
    if(it != MAP.end())
    {
    cout<<it->second<<endl;
    }
    return 0;
    }
      

  3.   

    你的代码没有问题,用find也没错,因为不知道存不存在。看你是怎么打印的。
      

  4.   

    我调试出问题啦,不知道怎么会是,string取到的值就是乱码,我在页面上做了一个类型转化,转化成CString,就能在页面上取得正确值。
    谢谢一楼,我以后有机会一定尝试一下你的方法