I want to print out the contents of a std::vector
in C++.
Here is what I have:
#include <iostream>
#include <iterator>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
vector<int> v;
copy_n(istream_iterator<int>(cin), 5, back_inserter(v));
return 0;
}
Can I print the contents of the std::vector
with the same method?
Yes. You need to iterate through the vector and std::copy
the contents to the output stream with the help of std::ostream_iterator .
std::copy(v.begin(), v.end(), std::ostream_iterator<int>(std::cout, " "));
是的,您可以通过将迭代器以及vector
和std::ostream_iterator
的大小传递给std::copy_n
。
std::copy_n(v.begin(), v.size(), std::ostream_iterator<int>(std::cout, " "));
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.