【问题标题】:How to access a vector through a pointer?如何通过指针访问向量?
【发布时间】:2012-10-17 08:23:20
【问题描述】:

我想使用指针将新元素插入vector 我有以下示例代码:

struct info {
    string Name;
    int places;  // i will use the binary value to identfy the visited places example 29 is 100101
                 // this means he visited three places (London,LA,Rome)  
    vector<int> times; // will represent the visiting time,e.g. 1,2,5 means london 1 time, LA
                       // twice and Rome five times
};

map<string,vector<info> *> log;

人们来自不同的城市,我会检查城市是否存在,只需将新人添加到vector,否则创建一个新的地图对象:

vector<info> tp;
info tmp;
if(log.size()==0|| log.count(city)==0) //empty or not exist 
{
    tp.push_back(tmp);
    vector<info>* ss = new vector<info>;
    ss=&(tp);
    // create a new object 
    log.insert(map<string,vector<info> * >::value_type(city,ss)); // new object 
}
else // city exist, just add the information to the vector
{
    map<string,vector<info> *>::iterator t;
    t=log.find(city);
    *(t->second).push_back(tmp);  //the problem  in this line
}

如何将新的 tmp 插入向量中?

需要阅读的信息如下:

Paris,Juli,5,3,6
Paris,John,24,2
Canberra,John,4,3
London,Mary,29,4,1,2

【问题讨论】:

  • 你提到问题在某行,但从不解释问题是什么。这是什么?
  • 删除程序中的所有星号,然后修复它以进行编译。你会过得更好。
  • 您是否建议他停止使用指针?请解释...
  • @mtsvetkov,我建议他停止使用指针,因为没有理由这样做。 map&lt;string,vector&lt;info&gt; *&gt; 应替换为 map&lt;string,vector&lt;info&gt; &gt;。事实上,前一个版本并非异常安全。另外,程序的问题其实出在ss=&amp;tp;,应该是*ss=tp,或者在我的版本中,ss=tp

标签: c++ pointers stdvector stdmap


【解决方案1】:

这里有很多错误,它们都源于滥用指针。作为问题原因提到的行是一个小的语法问题。手头有更大的问题。

所有这些都可以通过不滥用指针来轻松解决。这里没有理由使用指针,所以最终的解决方法是让地图有这种类型map&lt;string,vector&lt;info&gt;&gt; log;

那么代码就变成了这样:

info tmp;
log[city].push_back(tmp);
// the [] operator creates a new empty vector if it doesn't exist yet
// there's no point in doing the checks by hand

现在我们有了一个简单的解决方案,我将在 room 代码中提及大象。

vector<info>* ss = new vector<info>;
ss=&(tp);
// ...
log.insert(map<string,vector<info> * >::value_type(city,ss));

这一系列操作将创建一个具有动态存储持续时间的向量,并立即丢弃指向它的唯一指针。这导致刚刚创建的向量丢失,它使用的内存被泄露;再也无法恢复了。

更糟糕的是,它将ss 设置为指向一个局部变量,然后将该指针保存到映射中的局部变量。因为局部变量具有自动存储时长,所以一旦函数返回它就消失了。这使得刚刚存储在映射中的指针无效,因为它不再具有指向的向量。在那之后,就会造成各种破坏。

【讨论】:

  • 好像你不需要if:在这两种情况下你都需要log[city].push_back(tmp);
  • @Mr.C64 哦,你是对的。而且我认为我已经让代码变得如此简单了;)
  • 谢谢,这让 caod 变得如此简单,我需要打印我写的日志内容vector&lt;int&gt;::iterator j; for(i=log.begin();i!=log.end();i++) {cout&lt;&lt; "From "&lt;&lt; i-&gt;first&lt;&lt;" city people who travels: "&lt;&lt;endl; for (size_t tt = 0; tt &lt; (i-&gt;second).size(); tt++) { cout&lt;&lt; (i-&gt;second[tt]).Name&lt;&lt; " went to distnations "&lt;&lt; (i-&gt;second)[tt].places&lt;&lt;" \nwith the folloing number of visiting time "; for (j=((i-&gt;second[tt]).times).begin();j!= ((i-&gt;second[tt]).times).end();j++) cout&lt;&lt;*j&lt;&lt;" "; }}
  • 当我这样做时,屏幕上没有打印任何内容。我只想检查这些值是否正确插入到地图中
【解决方案2】:

看来你需要这样做

(t->second)->push_back(tmp);

【讨论】:

  • 不工作我得到了同样的错误信息((`push_back' 没有被声明))
  • 应该是(*t-&gt;second)-&gt;push_back(tmp);
猜你喜欢
  • 2014-07-26
  • 1970-01-01
  • 2021-12-10
  • 1970-01-01
  • 2012-06-07
  • 1970-01-01
  • 1970-01-01
  • 2015-10-18
  • 1970-01-01
相关资源
最近更新 更多