In practice, the vector series in c ++ -- copy set to vector (do not confuse reserve and resize)

Source: Internet
Author: User

In practice, the vector series in c ++ -- copy set to vector (do not confuse reserve and resize)

The stl algorithm has a copy function. We can easily write this Code:

#include 
  
   #include #include 
   
    using namespace std;int _tmain(int argc, _TCHAR* argv[]){ double darray[10]={1.0,1.1,1.2,1.3,1.4,1.5,1.6,1.7,1.8,1.9}; vector
    
      vdouble(10); vector
     
      ::iterator outputIterator=vdouble.begin(); copy(darray,darray+10,outputIterator); while(outputIterator!=vdouble.end()) {  cout<<*outputIterator<
      
     
    
   
  

So you want to use copy to set to vector, So you wrote:

#include  #include  #include  int main(){ std::set  input; input.insert(5); input.insert(6); std::vector  output; std::copy(input.begin(), input.end(), output.begin()); return 0;}     

Compiled successfully. An error occurs during running.
Why?

Method 1
If you specify the output size when defining the output, the error will disappear:

# Include  # Include  # Include  Int main () {std: set  Input; input. insert (5); input. insert (6); std: vector  Output (2); // specify the size std: copy (input. begin (), input. end (), output. begin (); std: cout <output. size () <std: endl; return 0 ;}     

Method 2: UseBack_inserter
Back_inserter is the iterator adapter that inserts elements to the end of a container as a real parameter.

#include  #include  #include  #include  int main(){ std::set  input; input.insert(5); input.insert(6); std::vector  output; std::copy(input.begin(), input.end(), std::back_inserter(output)); std::cout << output.size() << std::endl; return 0;}      

Continue vetor to vector:

#include  #include  #include  #include  int main(){ std::vector  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 here // Now v contains: { "first", "second", "first", "second" } return 0;}     

Let's look at this again: if we don't resize it, it will crash. If we replace resize with reserve, it will also crash and reconcile?

# Include  # Include  Using namespace std; int main () {int myints [] = {10, 20, 30, 40, 50, 60, 70}; vector  Myvector; vector  : Iterator it; // myvector. resize (7); // allocate space for the container myvector. Without resize, copy (myints, myints + 7, myvector. begin (); cout <"myvector contains:"; for (it = myvector. begin (); it! = Myvector. end (); ++ it) {cout <"" <* it;} cout <endl; return 0 ;}    

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

What is the difference between a vector with reserve and resize?

The vector reserve adds the vector capacity, but its size has not changed! Resize changes the capacity of the vector and increases its size!
The reason is as follows:
Reserve is a reserved container space, but does not actually create element objects in the space. Therefore, elements in the container cannot be referenced before a new object is added. When adding a new element, call the push_back ()/insert () function.

Resize is used to change the container size and create an object. Therefore, after calling this function, you can reference objects in the container. Therefore, when a new element is added, use the operator [] operator or the iterator to reference element objects. Now call the push_back () function, which is added after the new space. The parameter formats of the two functions are also different. The reserve function is followed by a parameter, that is, the space of the container to be reserved. The resize function can have two parameters. The first parameter is the new size of the container, the second parameter is to add a new element in the container. If this parameter is omitted, the default constructor of the element object is called.

Look at the instance:

# Include  # Include  Using namespace std; int main () {vector  Vector_for_reserve; vector  Vector_for_resize; vector_for_reserve.reserve (4); outputs (4); // size: 0 capacity: 4 cout <vector_for_reserve.size () <"<strong () <endl; // size: 4 capacity: 4 cout <vector_for_resize.size () <"" <vector_for_resize.capacity () <endl; vector_for_reserve [0] = 1; // error vector_for_resize [0] = 1; return 0 ;}    

Related Article

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.