Thread: mistake in tutorial re: stl vector

  1. #1
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,490

    mistake in tutorial re: stl vector

    in the tutorial at cprogramming.com (this site) on stl vectors it states:
    Vectors are more powerful than arrays because the number of functions that are available for accessing and modifying vectors. Unfortunately, the [] operator still does not provide bounds checking. Let's take a look at several functions provided by the vector class:

    Code:
    unsigned int size();            Returns the number of elements in a vector
    push_back(TYPE element);        Adds an element to the end of a vector
    swap(int x, int y);             Swap elements x and y
    bool empty();                   Returns true if the vector is empty
    void clear();                   Erase all elements of the vector
    ...

    Code:
    int main()
    {
      vector <int> example;        //Vector to store integers
      example.push_back(3);         //Add 3 onto the vector
      example.push_back(10);        //Add 10 to the end
      example.push_back(33);        //Add 33 to the end
      for(int x=0; x<example.size(); x++) 
      cout<<example[x]<<" ";        //Should output: 3 10 33
      if(!example.empty())          //Checks if empty
      example.clear();              //Clears vector
      vector <int> another_vector;  //Creates another vector to store integers
      another_vector.push_back(10); //Adds to end of vector
      example.push_back(10);        //Same
      if(example==another_vector)   //To show testing equality
      example.push_back(20); 
      example.switch(1, 2);         //Switches elements 1 and 2
      for(int y=0; y<example.size(); y++)
      cout<<example[y]<<" ";        //Should output 20 10
      return 0;
    }
    the above is clipped from this page


    is switch really a vector class function? i get errors because i think it's mistaking it as the keyword switch.
    is there some kind of swap function in vector arrays? if so, what's its syntax?

  2. #2
    Registered User Dual-Catfish's Avatar
    Join Date
    Sep 2001
    Posts
    802
    I believe the function is swap.

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >is switch really a vector class function?
    No, switch is a reserved keyword. The vector class does have a swap method, but it doesn't swap two elements in a vector, it swaps the entire contents of two vectors. If you want to swap two individual elements in one vector, use the swap method in <algorithm>.
    Code:
    #include <algorithm>
    #include <iostream>
    #include <vector>
    
    int main ( void )
    {
      std::vector<int> v;
      v.push_back ( 0 );
      v.push_back ( 1 );
      std::cout<<"v[0]: "<< v[0] <<"\n"<<"v[1]: "<< v[1] <<"\n";
      std::swap ( v[0], v[1] );
      std::cout<<"v[0]: "<< v[0] <<"\n"<<"v[1]: "<< v[1] <<"\n"; 
      return 0;
    }
    -Prelude
    My best code is written with the delete key.

  4. #4
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,490
    continuing on stl:
    is there algorithm function that can sort by a property of an object?

    ie:
    i understand sort to be like this:
    vector <int> vec_class;
    //push a bunch of stuff into vec_class
    sort (vec_class.begin(),vec_class.end());

    it sorts each vec_class int so that they line up numerically
    is there a way to make it sort by the property without resorting to a specialized function?
    vector<some_object* > vec_class;
    //push a bunch of object pointers, each having memory allocated to it
    vec_class[4]->property=3; //property is an int
    vec_class[2]->property=8;
    //etc...

    //sort array by value of each property

  5. #5
    Evil Member
    Join Date
    Jan 2002
    Posts
    638
    You would have to overload operator< for some_object. Make that compare whichever property you want.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Vector class -STL lib
    By nishkarsh in forum C++ Programming
    Replies: 4
    Last Post: 08-31-2008, 09:27 PM
  2. stl vector + iterator operation
    By sujeet1 in forum C++ Programming
    Replies: 1
    Last Post: 06-05-2007, 04:10 PM
  3. Vector class
    By Desolation in forum C++ Programming
    Replies: 2
    Last Post: 05-12-2007, 05:44 PM
  4. Unresolved external symbol using STL vector reference
    By raziel2k in forum C++ Programming
    Replies: 9
    Last Post: 04-01-2007, 04:28 PM
  5. Need some help/advise for Public/Private classes
    By nirali35 in forum C++ Programming
    Replies: 8
    Last Post: 09-23-2006, 12:34 PM