Shifting vectors to the left one time

do i have the right idea??

i just dont know how vectors work
1
2
3
4
5
6
 int temp = oldScores.at(0);
   for(i=0;i<SCORES_SIZE-1;i++)
      {
      oldScores.at(i) = oldScores.at(i+1);
      }
   oldScores.at(SCORES_SIZE-1) = temp;
it doesnt work
Is this what you had in mind?
1
2
3
4
5
6
7
8
9
#include <iostream>
#include <vector>

int main()
{
    std::vector<int> myVec {1, 2, 3, 4, 5, 6, 7, 8};
    myVec.erase(myVec.begin());
    for (const auto& elem : myVec)std::cout << elem << " ";
}

i just dont know how vectors work

Highly recommended: http://cppstdlib.com/
i dont think my teacher would accept this becuase we didnt learn the erase function
If the intention is to shift each element one position towards the front and put the first element at the back then the code looks fine. That is assuming SCORES_SIZE is equal to the size of the vector. You might not actually need SCORES_SIZE in your program at all because you can easily get the size of the vector using the size function: oldScores.size()

Note that C++ has a function template called std::rotate that does exactly this.
 
std::rotate(oldScores.begin(), oldScores.begin() + 1, oldScores.end());
http://www.cplusplus.com/reference/algorithm/rotate/
Last edited on
Hello seungyeon,

Combining what you started with and what gunnerfunner did I came up with:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
std::vector<int> oldScores{ 1, 2, 3, 4, 5, 6, 7, 8 };

std::cout << "\n Original Vector: ";

for (const auto& elem : oldScores)std::cout << elem << " ";

int temp = oldScores.at(0);
for (size_t i = 0; i < oldScores.size() - 1; i++)
{
	oldScores.at(i) = oldScores.at(i + 1);
}
oldScores.at(oldScores.size() - 1) = temp;

std::cout << "\n\n  Shifted Vector: ";

for (const auto& elem : oldScores)std::cout << elem << " ";


 Original Vector: 1 2 3 4 5 6 7 8

  Shifted Vector: 2 3 4 5 6 7 8 1

It worked fine shifting everything to the left one and putting 1 at the end.

Hope that is what you are looking for,

Andy
Nice. Now do yourself a favor.

i dont think my teacher would accept this becuase we didnt learn the erase function

Get over this mentality. Your professors can't teach you everything; I learned c++ decades ago and back then the rule of thumb was 5 HARD years in the language to master it. You get 1, maybe 1.5 years in college, and that only an hour a day.

Learn everything you can, even if you can't use it in the class, absorb it.
And your professor is more likely to be impressed with your initiative and willingness to research and learn than whether you can do it the hard way. The hard way is good practice, but its not good in practice :)

Hello seungyeon,

i just don't know how vectors work

You can think of a vector as being like an array just a different way in how it works. You can read up on vectors here at http://www.cplusplus.com/reference/vector/vector/?kw=vector

Also do a search here on "vector".

Or you can do a Google search on "c++ vector"

I agree with jonnin. Back in the day when I was learning "C" I was always doing something that was not thought in class and not only was my teacher impressed it was not a problem. Learn everything you can now because in the future you may not have the time to spend on learning.

Hope that helps,

Andy
Topic archived. No new replies allowed.