Thread: Real quick question on vectors, if I may

  1. #1
    Registered User
    Join Date
    Mar 2003
    Posts
    72

    Real quick question on vectors, if I may

    I was searching through the various posts regarding vectors and didn't see exactly what I was looking for, so...

    What I was wondering is how (if this is even possible) you can put the contents of a regular variable into a vector? For example, pretend I am doing the following (assuming I include the correct header files):

    Code:
    cout << "Enter some data => ";
    char SomeData;
    cin >> SomeData;
    
    // Declaring an empty vector
    vector<char> VectorData;
    Based on this, is there a way that I put the contents of SomeData into the vector named VectorData? I thought that you could perhaps do this with the vector's "insert" property (i.e. VectorData.insert(SomeData) ) , but that doesn't seem to work. Am I attempting to do something that is not possible or is it a simple case or user error? Any help that someone could provided would be much appreciated.

    Thanks, as always!

  2. #2
    Registered User
    Join Date
    Mar 2003
    Posts
    72
    I forgot to add something to my example... here's what it should look like:

    Code:
    cout << "Enter some data => ";
    char SomeData[50];
    cin >> SomeData;
    
    // Declaring an empty vector
    vector<char> VectorData;
    Or, it could be like this: (not sure if it really matters)

    Code:
    cout << "Enter some data =>";
    char SomeData[50];
    cin.getline(SomeData,50);
    
    // Declaring an empty vector
    vector<char> VectorData;
    Thanks!

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Not sure exactly why you would want a vector container for characters instead of just simply using a string container, but in answer to your question, you could use the push_back member function for each character the user inputs:

    Code:
    for(int i=0; i<50;++i)  // 50 or however many chars the user entered
        VectorData.push_back(SomeData[i]);
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  4. #4
    Registered User
    Join Date
    Mar 2003
    Posts
    72
    Awesome, thanks! I was going to mention that I thought the push_back property might be an option, but I wasn't real clear on how that worked? Perhaps using a string instead of char for the vector is a better idea, in retrospect...

    If I could ask one more question, under what circumstances would you use the the insert instead of push_back? I guess I am not clear on when insert is used...

    thanks!

  5. #5
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,829
    push_back() adds to the "back" or end of a vector. insert() allows insertion at random points.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  6. #6
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    Push back will always put the item at the end of the vector. You use insert if for some reason you want to place the item anywhere other than the end.
    Code:
    vector<int> myVec(10,0);  // creates vector of size 10, initialized to zero
    myVec.push_back(5);  // places 5 at the end of the vector
    myVec.insert(myVec.begin()+5,5); // inserts a 5 at index 5

  7. #7
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    The insert member function uses an iterator, basically a pointer to a location in the vector, as an argument to decide where the new element should be placed. For instance, consider inserting a value somewhere within a integer vector:

    Code:
    vector<int> intVect;
    vector<int>::iterator it;
    intVect.push_back(2);                               // intVect contains 2
    intVect.push_back(4);                               // intVect contains 2, 4
    intVect.push_back(6);                               // intVect contains 2, 4, 6
    intVect.push_back(10);                              // intVect contains 2, 4, 6, 10
    
    it = find(intVect.begin(),intVect.end(),10);        // Find the value '10' in vector
    
    it = intVect.insert(it,8);                          // intVect contains 2, 4, 6, 8, 10
    intVect.insert(it,3,7);                             // intVect contains 2, 4, 6, 7, 7, 7, 8, 10
    
    vector<int> intVect2;
    intVect2.push_back(5);                              // intVect2 contains 5
    intVect2.push_back(3);                              // intVect2 contains 5, 3
    it = find(intVect.begin(),intVect.end(),4);         // Find the value '4' in intVect
    intVect.insert(it,intVect2.begin(),intVect2.end()); // intVect contains 2, 5, 3, 4, 6, 7, 7, 7, 8, 10
    Bottomline is with the insert function, you need to provide the position where you wish to insert the value.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  8. #8
    Registered User
    Join Date
    Mar 2003
    Posts
    72
    Good deal.. I appreciate the information on all of that stuff. Thank you all very much.. you guys are a great resource for us noobs.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. %16 with double
    By spank in forum C Programming
    Replies: 11
    Last Post: 03-05-2006, 10:10 PM
  2. very quick question.
    By Unregistered in forum C++ Programming
    Replies: 7
    Last Post: 07-24-2002, 03:48 AM
  3. quick question
    By Unregistered in forum C++ Programming
    Replies: 5
    Last Post: 07-22-2002, 04:44 AM
  4. Quick Question Regarding Pointers
    By charash in forum C++ Programming
    Replies: 4
    Last Post: 05-04-2002, 11:04 AM
  5. Quick question: exit();
    By Cheeze-It in forum C Programming
    Replies: 6
    Last Post: 08-15-2001, 05:46 PM