如何打印vector <vector <string>&gt;在C ++中?</vector <string>

时间:2014-02-11 22:30:31

标签: c++ printing iterator

我得到一些运行C ++程序的值。说,

vector<vector<string>> clusters; // clusters have some values

如何使用迭代器打印它们?

3 个答案:

答案 0 :(得分:4)

希望迭代器的“使用”不需要明确且前期可见吗?

for( auto const& string_vec : clusters )
    for( auto const& s : string_vec )
        cout << s << endl;

答案 1 :(得分:2)

//iterate over our outer vector
for(vector<vector<string> >::iterator it = clusters.begin(); it != clusters.end(); ++it) { 
    //obtain and iterate over the inner vector
    for(vector<string>::iterator it2 = *it.begin(); it2 != *it.end(); ++it2) {
        cout << *it2; //obtain and print our string
    }
}

这会有用吗?

答案 2 :(得分:0)

同样,但有算法:

typedef std::vector<std::vector<std::string>> VectorVectorString;
typedef VectorVectorString::const_iterator VVSConstIterator;

VectorVectorString clusters;

for(VVSConstIterator pVector = clusters.begin(), end = clusters.end(); pVector != end; ++pVector)
{
    const std::vector<std::string>& current = *pVector;
    std::copy(
        current.begin(), current.end(),
        std::ostream_iterator<std::string>(std::cout, " "));
    std::cout << std::endl;
}