pairs of vector..

This is a discussion on pairs of vector.. within the C++ Programming forums, part of the General Programming Boards category; consider this : Code: typedef pair<char,char> twin; vector<twin> here; now how do I assign and/or access each vector and/or pair ...

  1. #1
    Burning in Hell! Luigi's Avatar
    Join Date
    Nov 2002
    Posts
    117

    pairs of vector..

    consider this :

    Code:
    typedef pair<char,char> twin;
    vector<twin> here;
    now how do I assign and/or access each vector and/or pair in here?

    I tried here.first and twin.first and both didnt worked...

    Luigi

  2. #2
    Registered User
    Join Date
    Jan 2002
    Posts
    552
    youll have to make sure there are values in the vector first. But once there are, you can index just like a normal array

    twin t;
    vector<twin> v;

    v.push_back(t);
    v[0]. // whatever methods pair contains
    C Code. C Code Run. Run Code Run... Please!

    "Love is like a blackhole, you fall into it... then you get ripped apart"

  3. #3
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    You use "first" and "second" to access the parts of the pair. Here is a simple example.

    Code:
    #include <utility>
    #include <iostream>
    using namespace std;
    
    // Less typing later..
    typedef struct pair< float, int > myPair;
    
    int main( int argc, char *argv[] )
    {
      // Use helpful function to assign values to pair
      myPair pair1 = make_pair( 25.5f, 5 );
    
      // Output values
      cout << pair1.first << "  " << pair1.second << endl;
    
      // Assign new values using first and second
      pair1.first = 101.2f;
      pair1.second = 55;
    
      // Output new values
      cout << pair1.first << "  " << pair1.second << endl;
    
      return 0;
    }

  4. #4
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Well the first reply explains how to use vectors the second explains pairs

    Just do as both people said.
    Code:
    my_pair_vect[0].first = whatever;

  5. #5
    Evil Member
    Join Date
    Jan 2002
    Posts
    638
    So shouldn't the title be vector of pairs?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem aligning floating point numbers
    By esbo in forum C Programming
    Replies: 4
    Last Post: 01-05-2009, 08:09 PM
  2. can some one please tell me the cause of the error ?
    By broli86 in forum C Programming
    Replies: 8
    Last Post: 06-26-2008, 09:36 PM
  3. syntax help?
    By scoobygoo in forum C++ Programming
    Replies: 1
    Last Post: 08-07-2007, 11:38 AM
  4. Need some help/advise for Public/Private classes
    By nirali35 in forum C++ Programming
    Replies: 8
    Last Post: 09-23-2006, 01:34 PM
  5. Certain functions
    By Lurker in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2003, 01:26 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21