C ++如何打印矢量<对<矢量<无符号>,矢量<无符号>>> VecPa

时间:2019-02-02 13:23:36

标签: c++

我该如何打印:vector<pair<vector<unsigned >, vector<unsigned > > > VecPa

我已经尝试过了,但是我不确定为什么它不起作用:

vector<pair<vector<unsigned >, vector<unsigned > > > :iterator it;
for (it=VecPa.begin(); it!=VecPa.end(); it++){

    vector<unsigned > VecPa=it->first;
    vector<unsigned > VecPa=it->second;

    for (int i=0; i<VecPa.size(); i++){
        cout<< " " << VecPa[i].first << ","<<VecPa[i].second<< "  ";

    }
    cout<<endl;
}

编译器说it是未声明的,并且已重新定义VecPa。为什么会这样?

1 个答案:

答案 0 :(得分:0)

  

只有一个VacPa变量

这是您的第一个VecPa:

for (it=VecPa.begin(); it!=VecPa.end(); it++){

这里是秒:

vector<unsigned > VecPa=it->first;

这里是第三个:

vector<unsigned > VecPa=it->second;

所以编译器说的很正确 VecPa有一个重新定义,它说是因为有了第三个定义,而已经有了第二个定义。


您希望在哪个VecPa中使用

for (int i=0; i<VecPa.size(); i++)

VecPa[i].firstVecPa[i].second中的哪个?


对我来说,您只是删除了意外的行

  vector<unsigned > VecPa=it->first;
  vector<unsigned > VecPa=it->second;

:iterator替换为::iterator,以更正嵌套中的访问,最后也许您只想要这样的东西:

int main()
{
  vector<pair<vector<unsigned >, vector<unsigned > > > VecPa;

  // populating VecPa

  vector<pair<vector<unsigned >, vector<unsigned > >  >::iterator it;
  for (it=VecPa.begin(); it!=VecPa.end(); it++){
    for (size_t i=0; (i < it->first.size()) && (i < it->second.size()) ; i++){
        cout << " " << it->first[i] << ","<<it->second[i] << "  ";
    }
    cout << endl;
  }
}

编译时无消息:

pi@raspberrypi:/tmp $ g++ -pedantic -Wall c.cc
pi@raspberrypi:/tmp $ 

例如,如果我填充:

#include <vector>
#include <iostream>
using namespace std;

int main()
{
  vector<pair<vector<unsigned >, vector<unsigned > >  > VecPa;

  vector<unsigned> v1 = { 1,2,3};
  vector<unsigned> v2 = { 11,22,33};
  vector<unsigned> v3 = { 111,222,333};
  vector<unsigned> v4 = { 1111,2222,3322};

  VecPa.push_back(pair<vector<unsigned >, vector<unsigned > > (v1,v2));
  VecPa.push_back(pair<vector<unsigned >, vector<unsigned > > (v3,v4));

  vector<pair<vector<unsigned >, vector<unsigned > > >::iterator it;

  for (it=VecPa.begin(); it!=VecPa.end(); it++){
    for (size_t i=0; (i < it->first.size()) && (i < it->second.size()) ; i++){
        cout << " " << it->first[i] << ","<<it->second[i] << "  ";
    }
    cout << endl;
  }
}

我有

pi@raspberrypi:/tmp $ g++ -pedantic -Wall c.cc
pi@raspberrypi:/tmp $ ./a.out
 1,11   2,22   3,33  
 111,1111   222,2222   333,3322  

valgrind 下:

pi@raspberrypi:/tmp $ valgrind ./a.out
==24555== Memcheck, a memory error detector
==24555== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==24555== Using Valgrind-3.13.0 and LibVEX; rerun with -h for copyright info
==24555== Command: ./a.out
==24555== 
 1,11   2,22   3,33  
 111,1111   222,2222   333,3322  
==24555== 
==24555== HEAP SUMMARY:
==24555==     in use at exit: 0 bytes in 0 blocks
==24555==   total heap usage: 12 allocs, 12 frees, 21,416 bytes allocated
==24555== 
==24555== All heap blocks were freed -- no leaks are possible
==24555== 
==24555== For counts of detected and suppressed errors, rerun with: -v
==24555== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 6 from 3)

要遵循新要求:

#include <vector>
#include <iostream>
using namespace std;

int main()
{
  vector<pair<vector<unsigned >, vector<unsigned > >  > VecPa;

  vector<unsigned> v1 = { 1,2,3};
  vector<unsigned> v2 = { 11,22,33};
  vector<unsigned> v3 = { 111,222,333};
  vector<unsigned> v4 = { 1111,2222,3322};

  VecPa.push_back(pair<vector<unsigned >, vector<unsigned > > (v1,v2));
  VecPa.push_back(pair<vector<unsigned >, vector<unsigned > > (v3,v4));

  vector<pair<vector<unsigned >, vector<unsigned > > >::const_iterator it;

  for (it=VecPa.begin(); it!=VecPa.end(); ++it) {
    vector<unsigned >::const_iterator it2;
    const char * sep;

    sep = "";    
    for (it2 = it->first.begin(); it2 != it->first.end(); ++it2) {
      cout << sep << *it2;
      sep = ",";
    }
    cout << " ; ";
    sep = ""; 
    for (it2 = it->second.begin(); it2 != it->second.end(); ++it2) {
      cout << sep << *it2;
      sep = ",";
    }
    cout << endl;
  }
}

编译和执行:

pi@raspberrypi:/tmp $ g++ -pedantic -Wall a.cc
pi@raspberrypi:/tmp $ ./a.out
1,2,3 ; 11,22,33
111,222,333 ; 1111,2222,3322

valgrind 下执行:

pi@raspberrypi:/tmp $ valgrind ./a.out
==31467== Memcheck, a memory error detector
==31467== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==31467== Using Valgrind-3.13.0 and LibVEX; rerun with -h for copyright info
==31467== Command: ./a.out
==31467== 
1,2,3 ; 11,22,33
111,222,333 ; 1111,2222,3322
==31467== 
==31467== HEAP SUMMARY:
==31467==     in use at exit: 0 bytes in 0 blocks
==31467==   total heap usage: 12 allocs, 12 frees, 21,416 bytes allocated
==31467== 
==31467== All heap blocks were freed -- no leaks are possible
==31467== 
==31467== For counts of detected and suppressed errors, rerun with: -v
==31467== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 6 from 3)