Data copy processing between a vector and an array

Source: Internet
Author: User

# Algostuff. h <br/>/* print_elements () <br/> *-prints optional c-string optcstr followed by <br/> *-all elements of the collection Coll <br/> *-separated by spaces <br/> * /<br/> template <class T> <br/> inline void print_elements (const T & Coll, const char * optcstr = "") <br/>{< br/> typename T: const_iterator Pos; <br/> STD: cout <optcstr; <br/> for (Pos = Coll. begin (); pos! = Coll. end (); ++ POS) {<br/> STD: cout <* POS <''; <br/>}< br/> STD :: cout <STD: Endl; <br/>}< br/>/* insert_elements (collection, first, last) <br/> *-fill values from first to last into the collection <br/> *-note: no half-open range <br/> */<br/> template <class T> <br/> inline void insert_elements (T & Coll, int first, int last) <br/> {<br/> for (INT I = first; I <= last; ++ I) {<br/> Coll. insert (Coll. end (), I); <br/>}< br/>}

 

// Test. cpp

# Include <vector> <br/> # include <string> <br/> # include <iostream> <br/> # include "algostuff. H "<br/> using namespace STD; </P> <p> int print (char c) <br/>{< br/> cout <C <Endl; <br/>}< br/> int main () <br/>{< br/> vector <char> V1 (20 ); <br/> string STR = "Hello World"; <br/> copy (Str. begin (), str. end (), v1.begin (); </P> <p> for_each (v1.begin (), v1.end (), print ); </P> <p> char Buf [32] = {0}; <br/> strcpy (BUF, & V1 [0]); <br/> cout <"Len:" <strlen (BUF) <Endl; <br/> cout <Buf <Endl; <br/> vector <int> V2; <br/> insert_elements (V2, 1, 10); <br/> int ibuf [10] = {0 }; <br/> memcpy (ibuf, & V2 [0], sizeof (INT) * v2.size (); </P> <p> for (INT I = 0; I <v2.size (); I ++) <br/>{< br/> cout <ibuf [I] <Endl; <br/>}</P> <p> vector <int> V3 (ibuf, ibuf + 10); <br/> print_elements (V3 ); <br/> return 0; <br/>}

 

 

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.