迭代指针矢量矢量

时间:2016-02-21 21:58:38

标签: c++ vector

大家好,这里是我的代码片段的一些部分。

我有一个包类

int s =10;
int *p=&s;
cout << p[3] << endl;

我创建了一个向量(包类型)

的向量
Packet::Packet(
    double sending_time,
    uint32_t seq_no,
    uint32_t size
)

//创建数据包并推送数据包

std::vector< std::vector<Packet*> > packets_for_tx_st

//回读数据包(回读数据包,我这里有问题)

for (int j=0;j<params.num_hosts;j++){
 for (uint32_t i = 0; i < params.num_packets_to_run; i++){
     Packet *p1 = new Packet(start_time, i, 64);  // Creating packets
     packets_for_tx_stat[j].push_back(p1);        // pushing packets  
    }

我在上面的步骤中遇到了问题。它会引发以下错误。

for(uint32_t i=0;i<params.num_hosts;i++){
for(uint32_t j = 0; j < packets_for_tx_stat[i].size(); j++ ){
    std::cout<< packets_for_tx_stats[i][j]->seq_no << "\t" << packets_for_tx_stats[i][j]->start_time << "\t" << std::endl;

在上述情况下,有人可以解释我缺少的内容以及打印矢量(指针)矢量的正确方法。

1 个答案:

答案 0 :(得分:0)

你的代码似乎很好,除了你在这里和那里有一些拼写错误。这是它的更正版本。

#include <iostream>
#include <vector>
#include <stdint.h>

using namespace std;

class Packet
{
public:
  Packet(double sending_time, uint32_t seq_no, uint32_t size);
  double sending_time;
  uint32_t seq_no;
  uint32_t size;
  uint32_t start_time;
};

Packet::Packet(
    double sending_time,
    uint32_t seq_no,
    uint32_t size
)
{
}



int main()
{
  std::vector< std::vector<Packet*> > packets_for_tx_stats;
  for(uint32_t i=0;i<20;i++){
    for(uint32_t j = 0; j < packets_for_tx_stats[i].size(); j++ ){
      std::cout<< packets_for_tx_stats[i][j]->seq_no << "\t" << packets_for_tx_stats[i][j]->start_time << "\t" << std::endl;
    }
  }
}