Vector series--copy set to vector in combat C + + (don't confuse reserve and resize)

Source: Internet
Author: User

There is a copy function in the STL algorithm. We can easily write this code:

#include <iostream>#include <algorithm>#include <vector>using namespace STD;int_tmain (intARGC, _tchar* argv[]) {Doubledarray[Ten]={1.0,1.1,1.2,1.3,1.4,1.5,1.6,1.7,1.8,1.9}; vector<double>Vdouble (Ten); vector<double>:: Iterator Outputiterator=vdouble.begin (); Copy (darray,darray+Ten, Outputiterator); while(Outputiterator!=vdouble.end ()) {cout<<*outputIterator<<endl; outputiterator++; } getchar ();return 0;}

So you want to use copy to set the to vector, so you write:

#include<iostream>#include<vector>#include<set>int main(){    std::set <double> input;    input.insert(5);    input.insert(6);    std::vector <double> output;    std::copy(input.begin(), input.end(), output.begin());    return0;}

Compile pass, error occurs when executing.
Why?

Method One
Suppose you specify its size when you define the output, and the error disappears:

#include <iostream>#include <vector>#include <set>intMain () {STD:: Set <double>Input Input.insert (5); Input.insert (6);STD:: vector <double>Output2);//Indicate size    STD:: Copy (Input.begin (), Input.end (), Output.begin ());STD::cout<< output.size () <<STD:: Endl;return 0;}

Method Two: Use back_inserter
Back_inserter is a iterator adapter that allows elements to be inserted at the end of a container as an actual participant

#include <iostream>#include <vector>#include <set>#include <iterator>intMain () {STD:: Set <double>Input Input.insert (5); Input.insert (6);STD:: vector <double>OutputSTD:: Copy (Input.begin (), Input.end (),STD:: Back_inserter (output));STD::cout<< output.size () <<STD:: Endl;return 0;}

Continue vetor to vector:

#include <iostream>#include <vector>#include <set>#include <iterator>int main () {STD::vector<STD::string> V, orig;Orig. Push_back ("First");Orig. Push_back ("Second");v = Orig;V. Insert(V. End(), V. Begin(), V. End());Now V contains: {"First","Second","",""} v = Orig;    STD:: Copy (v. Begin(), V. End(),STD:: Back_inserter (v));v = Orig;V. Reserve(V. Size() *2);V. Insert(V. End(), V. Begin(), V. End());Now V contains: {"First","Second","First","Second"} v = Orig;V. Reserve(V. Size() *2);    STD:: Copy (v. Begin(), V. End(),STD:: Back_inserter (v));Now V contains: {"First","Second","First","Second"}//Good (best): v = Orig;V. Insert(V. End(), orig. Begin(), orig. End());//note:we use different vectors hereNow V contains: {"First","Second","First","Second"} return0;}

Look at this again: Assuming not resize, will collapse, change resize to reserve will also collapse, reconciliation?

#include <iostream>#include <algorithm>#include <vector>using namespace STD;intMain () {intMyints[] = {Ten, -, -, +, -, -, -}; vector<int>Myvector; vector<int>:: Iterator it;//myvector.resize (7); Allocate space for container myvector, no resize will crashCopy (myints, myints +7, Myvector.begin ());cout<<"Myvector contains:"; for(it = Myvector.begin (); It! = Myvector.end (); ++it) {cout<<" "<< *it; }cout<< Endl;return 0;}

================================================================

The vector has a reserve of what resize. What is the difference?

The vector's reserve increases the capacity of the vector, but its size does not change! And resize changed the vector's capacity at the same time and added its size.
Reasons such as the following:
The reserve is a container-reserved space, but does not really create element objects in space, so you cannot reference the elements inside the container until you add new objects.

When you add a new element, you call the Push_back ()/insert () function.

  resize是改变容器的大小。且在创建对象,因此,调用这个函数之后。就能够引用容器内的对象了,因此当增加新的元素时。用operator[]操作符,或者用迭代器来引用元素对象。此时再调用push_back()函数,是加在这个新的空间后面的。

两个函数的參数形式也有差别的,reserve函数之后一个參数。即须要预留的容器的空间;resize函数能够有两个參数,第一个參数是容器新的大小, 第二个參数是要增加容器中的新元素,假设这个參数被省略,那么就调用元素对象的默认构造函数。

See Example:

#include <iostream>#include <algorithm>#include <vector>using namespace STD;intMain () { vector<int>Vector_for_reserve; vector<int>Vector_for_resize; Vector_for_reserve.reserve (4); Vector_for_resize.resize (4);//size:0 Capacity:4    cout<< vector_for_reserve.size () <<" "<< vector_for_reserve.capacity () << Endl;//size:4 Capacity:4    cout<< vector_for_resize.size () <<" "<< vector_for_resize.capacity () << Endl; vector_for_reserve[0] =1;//Errorvector_for_resize[0] =1;return 0;}

Vector series--copy set to vector in combat C + + (don't confuse reserve and resize)

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.