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.
Printable View
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.
Not sure what it is you are asking, but here is something for you "chew" on:
Output: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;
Hello World
Hello World
gg
>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
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.
>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:
-PreludeCode:#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;
}