How to swap the first and last elements of a vector using a function in C++?

  • Context: C/C++ 
  • Thread starter Thread starter Teh
  • Start date Start date
  • Tags Tags
    Parameter Vector
Click For Summary
SUMMARY

The discussion focuses on implementing a function called SwapVectorEnds() in C++ that swaps the first and last elements of a vector. The original implementation incorrectly loops through the entire vector, leading to unexpected output. The correct approach involves storing the last element in a temporary variable, assigning the first element's value to the last position, and then placing the temporary variable's value in the first position. This method ensures accurate swapping regardless of the vector's size.

PREREQUISITES
  • Understanding of C++ syntax and functions
  • Familiarity with the std::vector container
  • Knowledge of variable assignment and temporary storage
  • Basic debugging skills in C++
NEXT STEPS
  • Implement and test the corrected SwapVectorEnds() function
  • Explore C++ vector operations and their complexities
  • Learn about function overloading in C++
  • Investigate common pitfalls in C++ vector manipulation
USEFUL FOR

C++ developers, computer science students, and anyone interested in learning efficient vector manipulation techniques in C++.

Teh
Messages
47
Reaction score
0
What i am doing work because i can only get 40 30 30 40 as output?

Write a function SwapVectorEnds() that swaps the first and last elements of its vector parameter. Ex: sortVector = {10, 20, 30, 40} becomes {40, 20, 30, 10}. The vector's size may differ from 4.
Code:
#include <iostream>
#include <vector>
using namespace std;

/* Your solution goes here  */void SwapVectorEnds(vector<int>& sectorVector){ 
int i = 0;
for ( i = 0; i < sectorVector.size() - 1; ++i) {
  sectorVector.at(i) = sectorVector.at(sectorVector.size() - 1 - i);
}
   return;
}
   
   
   
   
   
int main() {
   vector<int> sortVector(4);
   int i = 0;

   sortVector.at(0) = 10;
   sortVector.at(1) = 20;
   sortVector.at(2) = 30;
   sortVector.at(3) = 40;

   SwapVectorEnds(sortVector);

   for (i = 0; i < sortVector.size(); ++i) {
      cout << sortVector.at(i) << " ";
   }
   cout << endl;

   return 0;
}
Testing with original sortVector = {10, 20, 30, 40}



Expected output:
40 20 30 10



Your output:
40 30 30 40
 
Technology news on Phys.org
You are looping over the entire vector, which isn't necessary. What I would do is first store the last element in a variable, put the first element's value into the last element, and then put the value of the variable we stored the last element's initial value into the first element. :)
 

Similar threads

  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 15 ·
Replies
15
Views
4K
  • · Replies 22 ·
Replies
22
Views
4K
  • · Replies 8 ·
Replies
8
Views
9K
  • · Replies 5 ·
Replies
5
Views
3K
Replies
12
Views
3K
  • · Replies 23 ·
Replies
23
Views
3K
  • · Replies 3 ·
Replies
3
Views
5K
Replies
20
Views
2K
  • · Replies 13 ·
Replies
13
Views
2K