2012-04-28 21 views
0

これは私が実行しているコードです:印刷内容

std::vector<std::vector<double>> test; 
test.push_back(std::vector<double>(30)); 

std::vector<std::vector<double> >::iterator it=test.begin(), end=test.end(); 
    while (it!=end) { 
     std::vector<double>::iterator it1=it->first.begin(),end1=it->first.end(); 
     while (it1!=end1) { 
    std::copy(it1.begin(),it1.end(),std::ostream_iterator<double>(std::cout, " ")); 
    ++it1; 
     } 
     ++it; 
    } 

これは私が手コンパイルエラーです:私は印刷することができますので、それを修正する方法について

data.cpp:33:45: error: ‘class std::vector<double>’ has no member named ‘first’ 
data.cpp:33:68: error: ‘class std::vector<double>’ has no member named ‘first’ 
data.cpp:35:16: error: ‘class std::vector<double>::iterator’ has no member named ‘begin’ 
data.cpp:35:28: error: ‘class std::vector<double>::iterator’ has no member named ‘end’ 
data.cpp:35:34: error: ‘ostream_iterator’ is not a member of ‘std’ 
data.cpp:35:56: error: expected primary-expression before ‘double' 

任意の提案テストの内容

+1

マイナーな提案:イテレータを使って2Dベクトルを変更するのではなく、その要素を読み込むだけで、@ sto :: vector :: const_iterator' –

+0

@BojanKomazecを使ってくれてありがとうございます。私はその理由のために修正 – user1155299

答えて

2

私はこれがあなたがもっと望んでいると思う。

std::vector<std::vector<double>> test; 
// Put some actual data into the test vector of vectors 
for(int i = 0; i < 5; ++i) 
{ 
    std::vector<double> random_stuff; 
    for(int j = 0; j < 1 + i; ++j) 
    { 
     random_stuff.push_back(static_cast<double>(rand())/RAND_MAX); 
    } 
    test.push_back(random_stuff); 
} 

std::vector<std::vector<double> >::iterator it=test.begin(), end=test.end(); 
while (it!=end) 
{ 
    std::vector<double>::iterator it1=it->begin(),end1=it->end(); 
    std::copy(it1,end1,std::ostream_iterator<double>(std::cout, " ")); 
    std::cout << std::endl; 
    ++it; 
} 

あなたのベクトルがペアが含まれていないので、あなたは最初にしたくない、と彼らはあなたがコピーする合格範囲を示すので、あなたはIT1とEND1に基づいてループする必要はありません。

2

コードには2つの問題があります。

まずstd::vectorsstd::pairsが含まれていないので、何のfirstまたはsecondありません:

while (it!=end) { 
    std::vector<double>::iterator it1=it->begin(),end1=it->end(); 

は、第二次std::copyへの呼び出しは、おそらくあなたの内側のベクトルのうちの1つに対応するべき範囲を、取ります。だからあなたは1つのレベルに行きすぎています。

外側のベクトルtestを繰り返し、その要素のそれぞれ(ベクトル)にcopyを使用して印刷できます。

std::vector<std::vector<double>> test; 
test.push_back(std::vector<double>(30)); 
std::vector<std::vector<double> >::iterator it=test.begin(), end=test.end(); 
for (it!= end, ++it) { 
    std::copy(it1-begin(),it->end(),std::ostream_iterator<double>(std::cout, " ")); 
} 
+0

を作っていきます。なんらかの理由で、forループはコンパイルエラーを出し、それをwhileに変更します。 – user1155299