std::vector Question
Hi there,
i have a vector, which contains, lets say, four elements:
1 2 3 4
now i want to move the vector so that "2" is at the beginning: All successive elements should move also the same distance, and the elements that were before this positions should fill the gap at the end:
2 3 4 1
how can i achieve this? is there a std:: function that easily performs such operations?
thanks for any help!
greets
Gammastrahler
I think, that there''s no std::function, that would do it for you, but it''s not so difficult do it yourself.
There ya go =)
Oxyd
---
- Unreadable code is code written on a piece of paper, but not the one, in which the programmer is using a space in the place you don''t.
- Real programmers aren''t afraid of goto
template <typename T>void rotateLeft (std::vector <T> &v){ T carry; carry = v [0]; //save the first element //shift all the elemets to the left for (int i = 0; i < v.size () - 1; ++i) v [i] = v [i + 1]; //set the last element to the saved value v [v.size () - 1] = carry; //that''s all =)}Now - there''s nothing easier but:int main (){ std::vector <int> v; v.push_back (1); v.push_back (2); v.push_back (3); v.push_back (4); std::cout << "Elements before rotating:\n"; for (int i = 0; i < v.size (); ++i) std::cout << v [i]; std::cout << ''\n''; rotateLeft (v); std::cout << "Elements after rotating:\n"; for (int i = 0; i < v.size (); ++i) std::cout << v [i]; std::cout ''\n''; return 0;}
There ya go =)
Oxyd
---
- Unreadable code is code written on a piece of paper, but not the one, in which the programmer is using a space in the place you don''t.
- Real programmers aren''t afraid of goto
Sure it exists...
Output:
[How To Ask Questions|STL Programmer''s Guide|Bjarne FAQ|C++ FAQ Lite|C++ Reference|MSDN]
#include <iostream>#include <vector>#include <string>#include <algorithm>#include <functional>int main(){ const int VECTOR_SIZE = 8; // Define a template class vector of strings typedef std::vector<string> StrVector; //Define an iterator for template class vector of strings typedef StrVector::iterator StrVectorIt; StrVector Tongue_Twister(VECTOR_SIZE); StrVectorIt start, end, middle, it; // location of first element of Tongue_Twister start = Tongue_Twister.begin(); // one past the location last element of Tongue_Twister end = Tongue_Twister.end(); // Initialize vector Tongue_Twister Tongue_Twister[0] = "she"; Tongue_Twister[1] = "sells"; Tongue_Twister[2] = "sea"; Tongue_Twister[3] = "shells"; Tongue_Twister[4] = "by"; Tongue_Twister[5] = "the"; Tongue_Twister[6] = "sea"; Tongue_Twister[7] = "shore"; middle = start + 3; // start position for rotating elements std::cout << "Before calling rotate" << std::endl; // print content of Tongue_Twister std::cout << "Try this Tongue Twister:"; for (it = start; it != end; ++it) cout << " " << *it; // rotate the items in the vector Tongue_Twister by 3 positions std::rotate(start, middle, end); std::cout << std::endl << "After calling rotate" << std::endl; // print content of Tongue_Twister std::cout << "Now try the rotated Tongue Twister:"; for (it = start; it != end; ++it) std::cout << " " << *it; std::cout << endl;}
Output:
Before calling rotateTry this Tongue Twister: she sells sea shells by the sea shoreAfter calling rotateNow try the rotated Tongue Twister: shells by the sea shore she sells sea
[How To Ask Questions|STL Programmer''s Guide|Bjarne FAQ|C++ FAQ Lite|C++ Reference|MSDN]
As stated previously, you can use
which outputs this:
[ Google || Start Here || ACCU || STL || Boost || MSDN || GotW || CUJ || MSVC++ Library Fixes || BarrysWorld || E-Mail Me ]
[edited by - Lektrix on April 11, 2004 11:49:33 AM]
std::rotate()
defined in algorithm
. Assuming I have understood your question correctly, this is how you would use this function:#include <iostream>#include <vector>#include <algorithm> int main(){ const std::vector<int>::size_type vec_size = 4; std::vector<int> vec(vec_size); for ( std::vector<int>::size_type ix = 0; ix < vec_size; ++ix ) vec[ix] = ix + 1; std::cout << "Before: "; std::copy( vec.begin(), vec.end(), std::ostream_iterator<int>(std::cout, " ") ); // operation here: std::rotate( vec.begin(), vec.begin() + 1, vec.end() ); std::cout << std::endl << std::endl << "After: "; std::copy( vec.begin(), vec.end(), std::ostream_iterator<int>(std::cout, " ") ); return 0;}
which outputs this:
Before: 1 2 3 4
After: 2 3 4 1
[ Google || Start Here || ACCU || STL || Boost || MSDN || GotW || CUJ || MSVC++ Library Fixes || BarrysWorld || E-Mail Me ]
[edited by - Lektrix on April 11, 2004 11:49:33 AM]
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement