如何在struct下打印出vector中的值

时间:2013-05-17 23:40:31

标签: c++ vector

在我的代码中,我无法打印出赋值给矢量的值。

#include <iostream>
#include <algorithm>
#include <string>
#include <iomanip>
#include <limits>
#include <stdio.h>
#include <sstream>
#include <vector>


using namespace std;
using std::stringstream;



string pMem, comment, sGen, val,input,id,size,inits,incs;

double pmemSize =0;

char t[10], m[256],init[10],inc[10];


struct rootset {
  double totSize;
  double *rStrtPtr;
  double *rEndPtr;
  vector<double> physicalM; /* This is the size of physical memory i need to assign*/

  struct generations {
    double totSize;
    const char *genStrtPtr;
    const char *genEndPtr; 
    int numOfGen;
    string genName;

    struct object {
      double objSize;
      const char *objStrtPtr;
      const char *objEndPtr;
      string id;
      char markBit;
      char objPtr;
    };

    struct freeList {
      double freeSpace;
      int flNumb; 
    };
  };
};
int main()
{
  int pmemSize;
  cout<<" ENter the size "<<endl;
  cin >> pmemSize;
  vector<rootset> pRootSet;
  pRootSet.push_back(rootset());
  pRootSet[0].totSize = pmemSize;
  pRootSet[0].physicalM.reserve(pmemSize);

   for (int s=0; s<pmemSize; ++s)
      pRootSet[0].physicalM[0] =s;

  vector<double>::iterator it;

  for(it = pRootSet[0].physicalM.begin(); it!= pRootSet[0].physicalM.end(); ++it) 
      cout <<"Printing it: " <<(*it)<<endl;
}

我基本上需要在用户提供的物理内存中分配一些空间。我以为我会用矢量。但是我无法打印我输入物理M的值。

2 个答案:

答案 0 :(得分:2)

问题是您没有正确地将值存储到physicalM中。使用:

    pRootSet[0].physicalM.push_back(s);

然后你的迭代器应该适当地打印它们。

答案 1 :(得分:1)

你拼错了物理M的索引:

for (int s=0; s<pmemSize; ++s)
      pRootSet[0].physicalM[s] =s;
                          ^^^^