c++ - Template Method 'For Loop' to Reverse Vector -


i have method reverses objects of vector. want able use method regardless of data type, i'm using template. ex : " 1 2 3 4 " becomes " 4 3 2 1" & "how brown cow" becomes "cow brown how"

this current template method.(full code added copy , paste purposes)

#include "stdafx.h"   #include <iostream>   using namespace std; template <class t> void fillvector(vector<t>& vect, const t array[], int size) { (int index = 0; index < size; ++index)     vect.push_back(array[index]); }  template<class t> void reverse(vector<t>& vect) {      if (vect.empty())     {         throw emptyvectorexception();     }      else     {          int endval = vect.size();          (int x = 0, y = endval-1; x < (endval / 2); x++, y--)         {             t temp = vect[x];             vect[x] = vect[y];             vect[y] = temp;         }     }  }   template<class t> void output(const vector<t>& vect) {     (int index = 0; index < static_cast<int>(vect.size()); index++)     {         cout << vect[index] << " ";     } }  int main() {  const string  strings[] = { "how ", "now ", "brown ", "cow" }; const char chars[] = { 'a', 'b', 'c', 'd', 'e', 'f' }; const int intnmbrs[] = { 1, 3, 5, 7, 9, 11, 13 }; const double dblnmbrs[] = { 11.1, 22.2, 33.3, 44.4, 55.5, 66.6 };  const int strings_size = sizeof(strings) / sizeof(strings[0]); const int chars_size = sizeof(chars) / sizeof(chars[0]); const int ints_size = sizeof(intnmbrs) / sizeof(intnmbrs[0]); const int doubles_size = sizeof(dblnmbrs) / sizeof(dblnmbrs[0]);  vector<int> intvector; vector<double> doublevector; vector<string> stringvector; vector<char> charvector;  // fill "vector" objects fillvector(intvector, intnmbrs, ints_size); fillvector(doublevector, dblnmbrs, doubles_size); fillvector(stringvector, strings, strings_size); fillvector(charvector, chars, chars_size);  vector<int> emptyvector;  cout << "\nexample #3: reverse sequence of \"vector\" objects"; reverse(intvector); reverse(doublevector); reverse(stringvector); reverse(charvector);  cout << "\n  \"intvector\":    "; output(intvector); cout << "\n  \"doublevector\": "; output(doublevector); cout << "\n  \"stringvector\": "; output(stringvector); cout << "\n  \"charvector\":   "; output(charvector); cout << endl;   return 0; } 

i met these errors :

'std::vector': many template arugments

'std::vector': no appropriate default constructor available

'x': undeclared identifier

'y': undeclared identifier

i have no idea why x , y aren't declared. integers used operate loop, , have no relation template t @ all.

i thought perhaps had way wrote reverse method, decided write reverse method deals vector if ints alone see if worked. vector test = { 1,2,3,4,5,6,7,8,9 };

int sizeof = test.size();      (int x = 0, y = sizeof-1; x < (sizeof / 2); x++, y--) {     int temp = test[x];     test[x] = test[y];     test[y] = temp; } 

this code worked fine. know has nothing how coded reverse algorithm.

i did searching , found 'std::vector': many template arugments error pops when try , pass template many parameters. don't believe case in situation(but wrong, i?)

as 'std::vector': no appropriate default constructor available, looks usual problem in case has having class default constructor being called (usually implicitly). not have class template, don't think(again, wrong. new) have constructors should called @ all.

thoughts?

have read no appropriate default constructor

and too many template parameter

i don't exact errors, i've marked relevant fixes below.

i ran code through astyle fix formatting.

//#include "stdafx.h" #include <iostream>  #include <vector>           //!fix #include <string>           //!fix  using namespace std; template <class t> void fillvector(vector<t>& vect, const t array[], int size) {     (int index = 0; index < size; ++index)         vect.push_back(array[index]); }  template<class t> void reverse(vector<t>& vect) {      if (vect.empty())     {         //throw emptyvectorexception;         return;     //!fix     }      else     {          int endval = vect.size();          (int x = 0, y = endval-1; x < (endval / 2); x++, y--)         {             t temp = vect[x];             vect[x] = vect[y];             vect[y] = temp;         }     }  }   template<class t> void output(const vector<t>& vect) {     (int index = 0; index < static_cast<int>(vect.size()); index++)     {         cout << vect[index] << " ";     } }  int main() {      const string  strings[] = { "how ", "now ", "brown ", "cow" };     const char chars[] = { 'a', 'b', 'c', 'd', 'e', 'f' };     const int intnmbrs[] = { 1, 3, 5, 7, 9, 11, 13 };     const double dblnmbrs[] = { 11.1, 22.2, 33.3, 44.4, 55.5, 66.6 };      const int strings_size = sizeof(strings) / sizeof(strings[0]);     const int chars_size = sizeof(chars) / sizeof(chars[0]);     const int ints_size = sizeof(intnmbrs) / sizeof(intnmbrs[0]);     const int doubles_size = sizeof(dblnmbrs) / sizeof(dblnmbrs[0]);      vector<int> intvector;     vector<double> doublevector;     vector<string> stringvector;     vector<char> charvector;  // fill "vector" objects     fillvector(intvector, intnmbrs, ints_size);     fillvector(doublevector, dblnmbrs, doubles_size);     fillvector(stringvector, strings, strings_size);     fillvector(charvector, chars, chars_size);      vector<int> emptyvector;      cout << "\nexample #3: reverse sequence of \"vector\" objects";     reverse(intvector);     reverse(doublevector);     reverse(stringvector);     reverse(charvector);      cout << "\n  \"intvector\":    ";     output(intvector);     cout << "\n  \"doublevector\": ";     output(doublevector);     cout << "\n  \"stringvector\": ";     output(stringvector);     cout << "\n  \"charvector\":   ";     output(charvector);     cout << endl;       return 0; } 

i think should better have been posted code review site.


Comments

Popular posts from this blog

wordpress - (T_ENDFOREACH) php error -

Export Excel workseet into txt file using vba - (text and numbers with formulas) -

Using django-mptt to get only the categories that have items -