C program used to swap the values about vector Please fix th

C++ program used to swap the values about vector. Please fix the problem. Please highlight the code which you rewrite. If I don\'t want use c++ 11 library how to write code. I want use g++ filename.cpp -o filename to check the code. Then use ./filename run the code. Please show me the new code.

This the error message.

lab8.cpp: In function ‘void printVec(const std::vector<T>&)’:
lab8.cpp:15:15: error: ‘iter’ does not name a type
for ( auto iter = vec.begin();iter != vec.end() ; iter++ ){
^
lab8.cpp:15:34: error: expected ‘;’ before ‘iter’
for ( auto iter = vec.begin();iter != vec.end() ; iter++ ){

#include <iostream>
#include <vector>
#include <string>
#include <stdlib.h>
#include <time.h>

using namespace std;

//Function that prints vector elements
template<class T>
void printVec( const vector<T> & vec){
//Initializing iterator
   typename vector<T>::iterator iter ;

   for ( auto iter = vec.gegin();iter != vec.end() ; iter++ ){
  
        cout << *iter << endl;
   }
}

//Function that shuffles vector elements
template<class B>
void shuffleVec(vector<B> & vec)
{

   int len = vec.size();
   int i, rPos;

    for (i=len-1; i>=0; i--){
       rPos = rand()%len;
       swap(vec.at(i), vec.at(rPos));
   }
}

//Main function
int main()
{
   //Initializing random function
   srand (time(NULL));

   cout<<\"\ \ Testing with double values: \ \";

   //GenericVector object of type double
   vector<double> doubleVec;

   //Inserting values into vector
   doubleVec.push_back(23.64);
   doubleVec.push_back(18.41);
   doubleVec.push_back(14.33);
   doubleVec.push_back(85.18);
   doubleVec.push_back(10.90);

   //Printing vector elements
   printVec(doubleVec);
  
   cout<<\"\ \ Testing with string values: \ \";
  
   //GenericVector object of type string
   vector<string> stringVec;
  
   //Insert three double values into vector
   stringVec.push_back(\"Anil\");
   stringVec.push_back(\"Manu\");
   stringVec.push_back(\"Varnitha\");
   stringVec.push_back(\"Sanjana\");
   stringVec.push_back(\"Tanvi\");

   //Printing vector elements
   printVec(stringVec);

   cout << \"\ \ String vector - After shuffle 1: \ \";

   //Shuffling elements
   shuffleVec(stringVec);

   //Printing vector elements
   printVec(stringVec);

   cout << \"\ \ String vector - After shuffle 2: \ \";

   //Shuffling elements
   shuffleVec(stringVec);
  
   //Printing vector elements
   printVec(stringVec);

   cout << \"\ \";
return 0;
}

Solution

You are getting error due to different c++ compiler version

auto support only c++11 or more verion

if you are using codeblock then follow step to setup c++11

step 1) open codeblock

step 2) click on setting menu bar.

step 3) clcik on compiler

step 4) select c++ 11 ISO language standard

step) click on OK.

----------------------

#include <iostream>
#include <vector>
#include <string>
#include <stdlib.h>
#include <time.h>
using namespace std;
//Function that prints vector elements
template<class T>
void printVec( const vector<T> & vec){
//Initializing iterator

typename std::vector<T>::iterator iter;

for ( auto iter = vec.begin(); iter != vec.end() ; iter++ ){

cout << *iter << endl;
}
}
//Function that shuffles vector elements
template<class B>
void shuffleVec(vector<B> & vec)
{
int len = vec.size();
int i, rPos;
for (i=len-1; i>=0; i--){
rPos = rand()%len;
swap(vec.at(i), vec.at(rPos));
}
}
//Main function
int main()
{
//Initializing random function
srand (time(NULL));
cout<<\"\ \ Testing with double values: \ \";
//GenericVector object of type double
vector<double> doubleVec;
//Inserting values into vector
doubleVec.push_back(23.64);
doubleVec.push_back(18.41);
doubleVec.push_back(14.33);
doubleVec.push_back(85.18);
doubleVec.push_back(10.90);
//Printing vector elements
printVec(doubleVec);

cout<<\"\ \ Testing with string values: \ \";

//GenericVector object of type string
vector<string> stringVec;

//Insert three double values into vector
stringVec.push_back(\"Anil\");
stringVec.push_back(\"Manu\");
stringVec.push_back(\"Varnitha\");
stringVec.push_back(\"Sanjana\");
stringVec.push_back(\"Tanvi\");
//Printing vector elements
printVec(stringVec);
cout << \"\ \ String vector - After shuffle 1: \ \";
//Shuffling elements
shuffleVec(stringVec);
//Printing vector elements
printVec(stringVec);
cout << \"\ \ String vector - After shuffle 2: \ \";
//Shuffling elements
shuffleVec(stringVec);

//Printing vector elements
printVec(stringVec);
cout << \"\ \";
return 0;
}

====================================================

output sample:-


Testing with double values:
23.64
18.41
14.33
85.18
10.9


Testing with string values:
Anil
Manu
Varnitha
Sanjana
Tanvi


String vector - After shuffle 1:
Sanjana
Tanvi
Manu
Anil
Varnitha


String vector - After shuffle 2:
Anil
Manu
Tanvi
Varnitha
Sanjana

---------------------------------------------------------------------------------------------

If you have any query, please feel free to ask.

Thanks a lot

C++ program used to swap the values about vector. Please fix the problem. Please highlight the code which you rewrite. If I don\'t want use c++ 11 library how t
C++ program used to swap the values about vector. Please fix the problem. Please highlight the code which you rewrite. If I don\'t want use c++ 11 library how t
C++ program used to swap the values about vector. Please fix the problem. Please highlight the code which you rewrite. If I don\'t want use c++ 11 library how t
C++ program used to swap the values about vector. Please fix the problem. Please highlight the code which you rewrite. If I don\'t want use c++ 11 library how t

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site