c++ - Store strings in a vector and sort them -
i want make class stores strings console in vector , sorts them alphabetically using selection sort. code far.
#include<iostream> #include<string> #include<vector> #include<algorithm> using namespace std; class dictionary { public: dictionary(); void read(vector<int>&words); void selectionsort(vector <int> &num); private: //vector<string> line_vector; string word; //vector<string> words; }; dictionary:: dictionary() { //line_vector = "<empty>"; string word = "<empty>"; } void dictionary:: read(vector<int>& words) { //vector<string> words; string word; while( cin >> word ) words.push_back(word); } //////////////////////////////////////////////////////////////////////////////////// void dictionary:: selectionsort(vector <int> &num) { int i, j, first, temp; int numlength = num.size( ); (i= numlength - 1; > 0; i--) { first = 0; // initialize subscript of first element (j=1; j<=i; j++) // locate smallest between positions 1 , i. { if (num[j] < num[first]) first = j; } temp = num[first]; // swap smallest found element in position i. num[first] = num[i]; num[i] = temp; } return; } void print(vector<dictionary>& a) { (int = 0; < a.size(); i++) cout << a[i]; cout << "\n"; } ///////////////////////////////////////////////////////////////////////////////////////////////////// int main() { dictionary dict; vector<dictionary> str; dict.read(str); dict.selectionsort(str); dict.print(str); return 0; }
and these errors:
in member function 'void dictionary::read(std::vector<int>&)': 31:46: error: no matching function call 'std::vector<int>::push_back(std::string&)' 31:46: note: candidates are: in file included /usr/include/c++/4.9/vector:64:0, 3: /usr/include/c++/4.9/bits/stl_vector.h:913:7: note: void std::vector<_tp, _alloc>::push_back(const value_type&) [with _tp = int; _alloc = std::allocator<int>; std::vector<_tp, _alloc>::value_type = int] push_back(const value_type& __x) ^ /usr/include/c++/4.9/bits/stl_vector.h:913:7: note: no known conversion argument 1 'std::string {aka std::basic_string<char>}' 'const value_type& {aka const int&}' /usr/include/c++/4.9/bits/stl_vector.h:931:7: note: void std::vector<_tp, _alloc>::push_back(std::vector<_tp, _alloc>::value_type&&) [with _tp = int; _alloc = std::allocator<int>; std::vector<_tp, _alloc>::value_type = int] push_back(value_type&& __x) ^
one of error lines seems helpful:
/usr/include/c++/4.9/bits/stl_vector.h:913:7: note: no known conversion argument 1 'std::string {aka std::basic_string<char>}' 'const value_type& {aka const int&}'
if pass argument doesn't match expected type function, c++ try find conversion passed type expected type. conversion include 1-argument constructors , cast operators. see http://www.cplusplus.com/doc/tutorial/typecasting/
this particular error indicating passed argument cannot converted int.
Comments
Post a Comment