2013-05-17 35 views
1

在我的代碼我無法打印出來分爲矢量值下打印出的矢量值。如何將結構

#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的值。

+0

您是否收到任何錯誤?他們是什麼? – 0x499602D2

回答

1

您physicalM的拼寫錯誤指數:

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

當然..謝謝。我如何使用迭代器打印出來?我明白這裏做錯了.. – Tuffy

+0

@BrownieTuffy你的迭代器的輸出循環看起來是正確的。它會產生什麼錯誤?究竟是什麼? – Inspired

+1

你應該使用'resize()'而不是'reserve()'或'push_back()'的值,如上面的回答中所建議的。 – Inspired

2

的問題是,你是不是存儲值到physicalM正常。使用:

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

然後你的迭代器應該適當地打印它們。