C + +: Print container (container) element using the copy function

Source: Internet
Author: User
Tags copy

C + + can use the elements in the Copy function output container (container) to replace the For loop.

Header file:

#include <algorithm>

#include <iterator>

Format:

Std::copy (Cont.begin (), Cont.end (),std::ostream_iterator<type> (Std::cout, ""));

Type is a kind that requires an output format, such as a container that is vector<int>, and the type is int.

Code:

* * * * * main.cpp * *  Created on:2014 June 17 
 *      author:spike/
      
#include <iostream >  
#include <algorithm>  
#include <iterator>  
#include <vector>  
      
using namespace Std;  
      
int main (void)  
{  
    Std::vector<int> VI = {1, 2, 3, 4, 5};  
    Std::copy (Vi.begin (), Vi.end (), std::ostream_iterator<int> (Std::cout, ""));  
    Std::cout << Std::endl;  
    return 0;  
}

Output:

1 2 3 4 5

Author: csdn Blog spike_king

More Wonderful content: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/cplus/

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.