繁体   English   中英

打印列表 std::vector 的向量<std::list<unsigned> &gt; </std::list<unsigned>

[英]printing vector of list std::vector<std::list<unsigned>>

修复:我在 header 文件中有错误的声明。 在我修复声明后,我能够构建和打印它。


我正在尝试打印列表向量,当我尝试以这 3 种方式构建时出现以下错误。 我错在哪里? 查看其他第 2 号帖子,建议 C++11 或更新版本为 3,而第 1 号是经典版本。 如何在 cpp 中使用 int 迭代器打印数组/列表的向量? )我还尝试使用 i.begin() 而不是 (*i).begin() 用于列表或 vec[i].begin(),但我得到其他类型的错误。

我不确定我有哪个 C++ 版本。

另一个参考( http://www.cplusplus.com/forum/beginner/75091/ )。

没有任何工作。 函数在 a.hpp 文件中声明

[ 10%] Building CXX object CMakeFiles/a.out.gtest.dir/app/proj1.cpp.o
[ 20%] Linking CXX executable bin/a.out.gtest
/usr/bin/ld: CMakeFiles/a.out.gtest.dir/app/proj1.cpp.o: in function `phaseTwo(std::__1::vector<std::__1::list<unsigned int, std::__1::allocator<unsigned int> >, std::__1::allocator<std::__1::list<unsigned int, std::__1::allocator<unsigned int> > > >&, unsigned int)':
/home/compsci161/projects/proj1/app/proj1.cpp:43: undefined reference to `printVec(std::__1::vector<std::__1::list<unsigned int, std::__1::allocator<unsigned int> >, std::__1::allocator<std::__1::list<unsigned int, std::__1::allocator<unsigned int> > > > const&)'
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[2]: *** [CMakeFiles/a.out.gtest.dir/build.make:114: bin/a.out.gtest] Error 1
make[1]: *** [CMakeFiles/Makefile2:80: CMakeFiles/a.out.gtest.dir/all] Error 2
make: *** [Makefile:84: all] Error 2

这是我的代码。 我尝试了不同的变化。 1号


int main(){

std::vector<std::list<unsigned>> vec = { 
            {1, 3, 5, 55, }, 
            {9, 20, 41, 75, }, 
            {44, 205, 453, 567, }, 
            {55, 245, 567, 890, }, 
            {100, 234, 456, 678, }, 
        };
}
void phaseTwo(std::vector<std::list<unsigned>> & vec, unsigned f)
{
    printVec(vec);

}
printVec(vec);
}

void printVec(std::vector<std::list<unsigned>>  vec)
{
    for(std::vector<std::list<int>>::iterator i = vec.begin(); i !=vec.end(); i++)  
    {
        for(std::list<int>::iterator iterList = (*i).begin(); iterList != (*i).end(); iterList++)   
        {
            std::cout << *iterList << ", ";        
        }   
        std::cout << "\n ";   
    }
}


2号


void printVec(std::vector<std::list<unsigned>>  vec)
{
    for (unsigned i = 0; i < vec.size(); i++) {
       for (auto e: vec[i]) {
            std::cout << e << ", ";
        }
        std::cout << "\n ";  
    }
}


3 号


void printVec(std::vector<std::list<unsigned>>  vec)
{
    for (auto i: vec) {
        for (auto e: i) {
            std::cout << e << ", ";
        }
        std::cout << "\n ";  
    }
}


你的第一个问题:

/home/compsci161/projects/proj1/app/proj1.cpp:43: undefined reference to `printVec(std::__1::vector<std::__1::list<unsigned int, std::__1::allocator<unsigned int> >, std::__1::allocator<std::__1::list<unsigned int, std::__1::allocator<unsigned int> > > > const&)'

undefined reference to printVec

是因为在您调用 function 时,编译器没有看到存在这样的 function 的声明。

            {100, 234, 456, 678, }, 
        };
printVec(vec);   // At this point.
                 // This is the first time the compiler has seen
                 // the function: printVec
}

通过声明 function 来解决这个问题。 或者将main()移动到程序的底部,使其写在printVec() function 之后。


另一个问题:

您正在将std::vector<std::list<unsigned>>传递给 function。

但是如果你仔细观察,你会得到一个迭代器:

 std::vector<std::list<int>>  // Notice int is not unsigned.

这里:

 for(std::vector<std::list<int>>::iterator i
                         ^^^^^^^^

 // and

 for(std::list<int>::iterator
             ^^^^^^^

为什么不使用版本 3。它旨在解决这个确切的问题。

其他方法是使用类型别名来表示类型。 这样,如果您在一个地方更改类型,它会在所有使用它的地方自动更改。

using ListOfData       = std::list<unsigned>;
using VectorOfDataList = std::vector<ListOfData>;

void printVec(VectorOfDataList  vec) {
    // STUFF
}

现在,如果您将基础数据的类型从unsigned更改为int ,您只需在一处和所有位置更改它(使用VectorOfDataList的位置将自动更改为使用新的基础类型。

一个基本的“代码异味”是由于一次更改需要在多个地方触摸代码而引起的。 您应该能够最好在一个地方更改内容,并且此更改将影响所有使用点。


你的循环有问题。
您总是在使用值(从而导致制作副本)。

void printVec(std::vector<std::list<unsigned>>  vec)
{
    for (auto i: vec) {
         //   ^^^^^   here i is a value.
         //           i.e. not a reference. So you are making
         //           a copy of the list into i.
        for (auto e: i) {
            std::cout << e << ", ";
        }
        std::cout << "\n ";  
    }
}

我会这样写:

void printVec(std::vector<std::list<unsigned>>  vec)
{
    for (auto const& i: vec) {       // Notice the const&
        for (auto const& e: i) {     // Notice the const& (not strictly required
                                     // but makes using the for loop consistent.
            std::cout << e << ", ";
        }
        std::cout << "\n ";  
    }
}

作为旁注。

您意识到您正在按值传递向量。 因此,副本由整个向量(以及所有包含的列表)组成。

// Pass by value.
void printVec(std::vector<std::list<unsigned>>  vec)


// Pass by const reference.
void printVec(std::vector<std::list<unsigned>> const& vec)
// Because it is a reference the compiler does not need to make a copy.

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM