vector中保存字符指针的总结

vector< char * >奇遇

  vector,相信接触过c++的开发人员都不陌生。可以简单的理解为其是能够动态扩展的数组。闲话少叙,接下来描述曾经遇到过的一个问题。
  打算使用vector来保存一批字符串,自然而然的想到了使用如下方式:

vector<char *> vchar;

vchar用来保存字符串。先来个示例:

int main()
{
    vector<char *> vchar;
    vchar.push_back(const_cast<char *>("hello"));
    vchar.push_back(const_cast<char *>("world"));
    vchar.push_back(const_cast<char *>("welcome"));

    vector<char *>::iterator begin = vchar.begin();
    vector<char *>::iterator end = vchar.end();
    while (begin != end)
    {
        cout << *begin++ <<endl;
    }

    return 0;
}

输出结果

hello
world
welcome

vchar中保存的值可能如下所示(第一行为索引,第二行为指针):

0123
0x401ed30x401ed90x401edf

内存分布可能如下:

指针内存内容
0x401ed3h
e
l
l
o
0
0x401ed9w
o
r
l
d
0
0x401edfw
e
l
c
o
m
e
0

下面看一下异常的代码

int main()
{
    vector<char *> vc;
    stringstream ss;

    for (int i = 0; i < 3; ++i)
    {
        ss << i;
        vc.push_back(const_cast<char *>(ss.str().c_str()));
        ss.str("");
    }

    vector<char *>::iterator it;
    for (it = vc.begin(); it != vc.end(); ++it)
        cout << *it << endl;

    return 0;
}

结果出乎意料,竟然是3个2

2
2
2

猛然一看,挺困惑,静下来一分析,嗯嗯,原来如此啊。如下:
vc中存储的是指针,可能如下:

0123
0x1bee2580x1bee2580x1bee258

vector中的元素均指向同一个地址,在循环结束时,这块内存中的值也就变成了2,所以它们的值自然也就是同一个了。
找到了原因,问题自然就好解决了。一种方法,每次放入不同的指针。更简单的一种方案是,使用vector< string>来代替vector< char *>。

int main()
{
    vector<string> vs;
    stringstream ss;

    for (int i = 0; i < 3; ++i)
    {
        ss << i;
        vs.push_back(ss.str());  // 每次放入一个新的string对象
        ss.str("");
    }

    vector<string>::iterator it;
    for (it = vs.begin(); it != vs.end(); ++it)
    {
        cout << *it << endl;
        cout << static_cast<const void *>(&(*it)) << endl;
    }

    return 0;
}

输出结果可能如下:

0
0x95d310
1
0x95d318
2
0x95d320

可以看到,三个对象的地址明显不是同一个,表明vs中存放的是三个不同的对象。

使用vector或其他容器的时候,如果其保存的是指针类型的数据,一定要留心。

  • 4
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值