-1

I have some questions about vector ;

  • How can I append one vector in another end in time efficient way ?
  • How can I sort one vector over using STL feature if type of it is NODE ? ( for NODE, look below )

    ex

    struct NODE {
    
               char name;
    
               .... // other things
    
    };
    
oscego
  • 349
  • 1
  • 2
  • 5
  • 1
    Well that's two unrelated questions. One at a time please. (And for both: what have you tried/looked into, and what didn't work out?) – Mat May 01 '12 at 10:54
  • 1
    For your first question see http://stackoverflow.com/questions/2551775/c-appending-a-vector-to-a-vector and for your second question see: http://forums.codeguru.com/showthread.php?t=366064 – Digital Da May 01 '12 at 11:00
  • Have you tried searching [`std vector append`](http://www.google.co.uk/search?q=std+vector+append), or [`std vector sort`](http://www.google.co.uk/search?q=std+vector+sort)? – Peter Wood May 01 '12 at 11:20

2 Answers2

2
  • std::vector::insert() can be used to insert from anything which uses the iterator protocol
  • std::sort() sorts in ascending order and takes an optional comparison function (or anything with operator() on it) which takes elements a and b and should return true if a < b.
Electro
  • 2,994
  • 5
  • 26
  • 32
2

To append one vector to another:

myVec.insert(myVec.end(), myVec2.begin(), myVec2.end());
// if your compiler supports C++11 you can do:
myVec.insert(end(myVec), begin(myVec2), end(myVec2));

To sort your vector:

vector<Node> myVec;
// add elements...
sort(myVec.begin(), myVec.end());

Node should define the < operator:

bool operator<(const Node & rhs, const Node & lhs){
    return rhs.name < lhs.name;
}
Uflex
  • 1,396
  • 1
  • 13
  • 32