vector insert functions

This is a discussion on vector insert functions within the C++ Programming forums, part of the General Programming Boards category; I've been trying to use the vector insert functions to insert white spaces at the front of my string vector, ...

  1. #1
    Registered User
    Join Date
    Dec 2001
    Posts
    104

    vector insert functions

    I've been trying to use the vector insert functions to insert white
    spaces at the front of my string vector, but I can't get it to work.
    Will anyone provide some simple exampe(s) of how these functions are or can be used?
    Thanks.

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,669
    Not sure what it is you are asking, but here is something for you "chew" on:
    Code:
        vector<string> v;
        v.push_back("World");
        v.push_back("World");
    
        v[0] = "Hello " + v[0];
        v[1].insert(0,"Hello ");
    
        cout << v[0] << endl;
        cout << v[1] << endl;
    Output:
    Hello World
    Hello World

    gg

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >I've been trying to use the vector insert functions to insert white spaces at the front of my string vector
    If you are doing a great deal of insertions at the front of the vector then you should be using a deque. vector insertions anywhere except the end tend to be expensive.

    -Prelude
    My best code is written with the delete key.

  4. #4
    Registered User
    Join Date
    Dec 2001
    Posts
    104
    Prelude:
    What do you mean by expensive?

    In any event, what it isn't working for me. What I'm really trying to do is move the alpha characters to the end of say a 8 space/charcter long vector, or decque if that will work better.

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >What do you mean by expensive?
    To add at the front of a vector, all of the current elements have to be shifted down by one to make room. This memory shift is very inefficient when compared to a deque, where insertion at the front is immediate:
    Code:
    #include <iostream>
    #include <vector>
    #include <deque>
    
    using namespace std;
    
    void print ( vector<char>& vc, deque<char>& dc )
    {
      for ( vector<char>::size_type i = 0; i < vc.size(); i++ )
        cout<< vc[i];
    
      cout<<endl;
    
      for ( deque<char>::size_type i = 0; i < dc.size(); i++ )
        cout<< dc[i];
    
      cout<<endl;
    }
    
    int main()
    {
      vector<char> vc ( 5, 'a' );
      deque<char> dc ( 5, 'a' );
    
      print ( vc, dc );
    
      vc.insert ( vc.begin(), 5, ' ' ); // Expensive
      dc.insert ( dc.begin(), 5, ' ' ); // Cheap
    
      print ( vc, dc );
    
      return 0;
    }
    -Prelude
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Void Functions Help
    By bethanne41 in forum C++ Programming
    Replies: 1
    Last Post: 05-09-2005, 05:30 PM
  2. Functions and Classes - What did I do wrong?
    By redmage in forum C++ Programming
    Replies: 5
    Last Post: 04-11-2005, 11:50 AM
  3. calling functions within functions
    By edd1986 in forum C Programming
    Replies: 3
    Last Post: 03-29-2005, 02:35 AM
  4. Factory Functions HOWTO
    By GuardianDevil in forum Windows Programming
    Replies: 1
    Last Post: 05-01-2004, 01:41 PM
  5. Shell functions on Win XP
    By geek@02 in forum Windows Programming
    Replies: 6
    Last Post: 04-19-2004, 05:39 AM

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