How to print out the contents of a vector?

后端 未结 19 1196
旧时难觅i
旧时难觅i 2020-11-22 03:46

I want to print out the contents of a vector in C++, here is what I have:

#include 
#include 
#include 
#include         


        
相关标签:
19条回答
  • 2020-11-22 04:31

    In C++11 you can now use a range-based for loop:

    for (auto const& c : path)
        std::cout << c << ' ';
    
    0 讨论(0)
  • 2020-11-22 04:32

    I think the best way to do this is to just overload operator<< by adding this function to your program:

    #include <vector>
    using std::vector;
    #include <iostream>
    using std::ostream;
    
    template<typename T>
    ostream& operator<< (ostream& out, const vector<T>& v) {
        out << "{";
        size_t last = v.size() - 1;
        for(size_t i = 0; i < v.size(); ++i) {
            out << v[i];
            if (i != last) 
                out << ", ";
        }
        out << "}";
        return out;
    }
    

    Then you can use the << operator on any possible vector, assuming its elements also have ostream& operator<< defined:

    vector<string>  s = {"first", "second", "third"};
    vector<bool>    b = {true, false, true, false, false};
    vector<int>     i = {1, 2, 3, 4};
    cout << s << endl;
    cout << b << endl;
    cout << i << endl;
    

    Outputs:

    {first, second, third}
    {1, 0, 1, 0, 0}
    {1, 2, 3, 4}
    
    0 讨论(0)
  • 2020-11-22 04:37

    A much easier way to do this is with the standard copy algorithm:

    #include <iostream>
    #include <algorithm> // for copy
    #include <iterator> // for ostream_iterator
    #include <vector>
    
    int main() {
        /* Set up vector to hold chars a-z */
        std::vector<char> path;
        for (int ch = 'a'; ch <= 'z'; ++ch)
            path.push_back(ch);
    
        /* Print path vector to console */
        std::copy(path.begin(), path.end(), std::ostream_iterator<char>(std::cout, " "));
    
        return 0;
    }
    

    The ostream_iterator is what's called an iterator adaptor. It is templatized over the type to print out to the stream (in this case, char). cout (aka console output) is the stream we want to write to, and the space character (" ") is what we want printed between each element stored in the vector.

    This standard algorithm is powerful and so are many others. The power and flexibility the standard library gives you are what make it so great. Just imagine: you can print a vector to the console with just one line of code. You don't have to deal with special cases with the separator character. You don't need to worry about for-loops. The standard library does it all for you.

    0 讨论(0)
  • 2020-11-22 04:37

    Just copy the container to the console.

    std::vector<int> v{1,2,3,4};
    std::copy(v.begin(),v.end(),std::ostream_iterator<int>(std::cout, " " ));
    

    Should output :

    1 2 3 4
    
    0 讨论(0)
  • 2020-11-22 04:37

    The problem is probably in the previous loop: (x = 17; isalpha(firstsquare); x++). This loop will run not at all (if firstsquare is non-alpha) or will run forever (if it is alpha). The reason is that firstsquare doesn't change as x is incremented.

    0 讨论(0)
  • 2020-11-22 04:37

    This answer is based on the answer from Zorawar, but i couldn't leave a comment there.

    You can make the auto (C++11)/typedef version const by using cbegin and cend instead

    for (auto i = path.cbegin(); i != path.cend(); ++i)
        std::cout << *i << ' ';
    
    0 讨论(0)
提交回复
热议问题