+ Reply to Thread
Results 1 to 2 of 2

Thread: How to merge to different STL vector objects in C++?

  1. #1
    Junior Member
    Join Date
    May 2012
    Posts
    29

    How to merge to different STL vector objects in C++?

    How to merge to different STL vector objects in C++?

    This is my code:

    Code:
    void ConcateVectors()
    {
    	std::vector<int> vecInts1 ;
    
    	vecInts1.push_back(10) ;
    	vecInts1.push_back(20) ;
    	vecInts1.push_back(30) ;
    	vecInts1.push_back(40) ;
    	vecInts1.push_back(50) ;
    
    	std::vector<int> vecInts2 ;
    
    	vecInts1.push_back(60) ;
    	vecInts1.push_back(70) ;
    	vecInts1.push_back(80) ;
    	vecInts1.push_back(90) ;
    	vecInts1.push_back(100) ;
    	
    	std::vector<int>::iterator itr = vecInts1.begin() ;
    	for (; itr != vecInts1.end(); itr++)
    	{
    		vecInts2.push_back(*itr) ;
    	}
    }
    Is it good approach?

    How can I improve this?

  2. #2
    Junior Member
    Join Date
    Apr 2012
    Posts
    38
    You can use stl::vector::insert method.

    Example:

    Code:
    void MergeVectors()
    {
    	std::vector<int> vecInts1 ;
    
    	vecInts1.push_back(10) ;
    	vecInts1.push_back(20) ;
    	vecInts1.push_back(30) ;
    	vecInts1.push_back(40) ;
    	vecInts1.push_back(50) ;
    
    	std::vector<int> vecInts2 ;
    
    	vecInts1.push_back(60) ;
    	vecInts1.push_back(70) ;
    	vecInts1.push_back(80) ;
    	vecInts1.push_back(90) ;
    	vecInts1.push_back(100) ;
    	
    	vecInts1.insert(vecInts1.end(), vecInts2.begin(), vecInts2.end()) ;
    }

+ Reply to Thread

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts