• Advertisement

Archived

This topic is now archived and is closed to further replies.

std::vector Question

This topic is 5061 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
Advertisement
I think, that there''s no std::function, that would do it for you, but it''s not so difficult do it yourself.


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

Share this post


Link to post
Share on other sites
Sure it exists...

#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 rotate
Try this Tongue Twister: she sells sea shells by the sea shore
After calling rotate
Now 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]

Share this post


Link to post
Share on other sites
As stated previously, you can use 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]

Share this post


Link to post
Share on other sites

  • Advertisement