vector als String
-
Hallo,
ich versuche mich seit neuestem an Vectoren. jetzt brauche ich ein Vector, der Strings speichert.
mein Code:
#include <iostream> // für Eingabe, Ausgabe in der Console #include <string> // um Strings zu erstellen #include <fstream> //um Dateien zu lesen und zu schreiben #include <vector> //für vector #include <algorithm> //für std::sort int main(){ std::string s; //Variable S als String deklarieren std::vector<int, std::string> load; //Ein Vector deklarieren für später zum sortieren std::ifstream fil("wordlist.txt"); //einen In Stream mit einer Textdatei deklariert if(fil){ //Überprüfe ob Datei vorhanden while(fil){ //wenn Ja Schleife ausführen std::getline(fil,s); // Datei Zeilenweiße einlesen und in s speichern load.push_back(s); //s in vector speichern } }else{ //wenn keine Datei vorhanden, Fehlermeldung std::cout << "No File loaded!" << std::endl; } std::sort (load.begin(), load.end()); //vector sortieren for(int idx=0; idx <= load.size(); ++idx){ std::cout << load[idx] << endl; } }
jetzt meckert er sehr viel:
In file included from /usr/include/c++/5/bits/basic_string.h:40:0,
from /usr/include/c++/5/string:52,
from /usr/include/c++/5/bits/locale_classes.h:40,
from /usr/include/c++/5/bits/ios_base.h:41,
from /usr/include/c++/5/ios:42,
from /usr/include/c++/5/ostream:38,
from /usr/include/c++/5/iostream:39,
from main.cpp:1:
/usr/include/c++/5/ext/alloc_traits.h: In instantiation of ‘struct __gnu_cxx::__alloc_traits<std::__cxx11::basic_string<char> >::rebind<int>’:
/usr/include/c++/5/bits/stl_vector.h:75:28: required from ‘struct std::_Vector_base<int, std::__cxx11::basic_string<char> >’
/usr/include/c++/5/bits/stl_vector.h:214:11: required from ‘class std::vector<int, std::__cxx11::basic_string<char> >’
main.cpp:9:32: required from here
/usr/include/c++/5/ext/alloc_traits.h:208:62: error: no class template named ‘rebind’ in ‘class std::__cxx11::basic_string<char>’
{ typedef typename _Alloc::template rebind<_Tp>::other other; };
^
In file included from /usr/include/c++/5/vector:64:0,
from main.cpp:4:
/usr/include/c++/5/bits/stl_vector.h: In instantiation of ‘class std::vector<int, std::__cxx11::basic_string<char> >’:
main.cpp:9:32: required from here
/usr/include/c++/5/bits/stl_vector.h:241:20: error: no members matching ‘std::vector<int, std::__cxx11::basic_string<char> >::_Base {aka std::_Vector_base<int, std::__cxx11::basic_string<char> >}::_M_allocate’ in ‘std::vector<int, std::__cxx11::basic_string<char> >::_Base {aka struct std::_Vector_base<int, std::__cxx11::basic_string<char> >}’
using _Base::_M_allocate;
^
/usr/include/c++/5/bits/stl_vector.h:242:20: error: no members matching ‘std::vector<int, std::__cxx11::basic_string<char> >::_Base {aka std::_Vector_base<int, std::__cxx11::basic_string<char> >}::_M_deallocate’ in ‘std::vector<int, std::__cxx11::basic_string<char> >::_Base {aka struct std::_Vector_base<int, std::__cxx11::basic_string<char> >}’
using _Base::_M_deallocate;
^
/usr/include/c++/5/bits/stl_vector.h:244:20: error: no members matching ‘std::vector<int, std::__cxx11::basic_string<char> >::_Base {aka std::_Vector_base<int, std::__cxx11::basic_string<char> >}::_M_get_Tp_allocator’ in ‘std::vector<int, std::__cxx11::basic_string<char> >::_Base {aka struct std::_Vector_base<int, std::__cxx11::basic_string<char> >}’
using _Base::_M_get_Tp_allocator;
^
main.cpp: In function ‘int main()’:
main.cpp:16:19: error: no matching function for call to ‘std::vector<int, std::__cxx11::basic_string<char> >::push_back(std::__cxx11::string&)’
load.push_back(s); //s in vector speichern
^
In file included from /usr/include/c++/5/vector:64:0,
from main.cpp:4:
/usr/include/c++/5/bits/stl_vector.h:913:7: note: candidate: void std::vector<_Tp, _Alloc>::push_back(const value_type&) [with _Tp = int; _Alloc = std::__cxx11::basic_string<char>; std::vector<_Tp, _Alloc>::value_type = int]
push_back(const value_type& __x)
^
/usr/include/c++/5/bits/stl_vector.h:913:7: note: no known conversion for argument 1 from ‘std::__cxx11::string {aka std::__cxx11::basic_string<char>}’ to ‘const value_type& {aka const int&}’
main.cpp:22:18: error: ‘class std::vector<int, std::__cxx11::basic_string<char> >’ has no member named ‘begin’
std::sort (load.begin(), load.end()); //vector sortieren
^
main.cpp:22:32: error: ‘class std::vector<int, std::__cxx11::basic_string<char> >’ has no member named ‘end’
std::sort (load.begin(), load.end()); //vector sortieren
^
main.cpp:23:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int idx=0; idx <= load.size(); ++idx){
^
main.cpp:24:20: error: no match for ‘operator[]’ (operand types are ‘std::vector<int, std::__cxx11::basic_string<char> >’ and ‘int’)
std::cout << load[idx] << endl;
^
main.cpp:24:29: error: ‘endl’ was not declared in this scope
std::cout << load[idx] << endl;
^
main.cpp:24:29: note: suggested alternative:
In file included from /usr/include/c++/5/iostream:39:0,
from main.cpp:1:
/usr/include/c++/5/ostream:590:5: note: ‘std::endl’
endl(basic_ostream<_CharT, _Traits>& __os)
^
In file included from /usr/include/c++/5/vector:64:0,
from main.cpp:4:
/usr/include/c++/5/bits/stl_vector.h: In instantiation of ‘std::vector<_Tp, _Alloc>::~vector() [with _Tp = int; _Alloc = std::__cxx11::basic_string<char>]’:
main.cpp:9:32: required from here
/usr/include/c++/5/bits/stl_vector.h:424:22: error: ‘struct std::_Vector_base<int, std::__cxx11::basic_string<char> >::_Vector_impl’ has no member named ‘_M_start’
{ std::_Destroy(this->_M_impl._M_start, this->_M_impl._M_finish,
^
/usr/include/c++/5/bits/stl_vector.h:424:22: error: ‘struct std::_Vector_base<int, std::__cxx11::basic_string<char> >::_Vector_impl’ has no member named ‘_M_finish’
/usr/include/c++/5/bits/stl_vector.h:425:28: error: ‘_M_get_Tp_allocator’ was not declared in this scope
_M_get_Tp_allocator()); }
^
/usr/include/c++/5/bits/stl_vector.h: In instantiation of ‘std::vector<_Tp, _Alloc>::size_type std::vector<_Tp, _Alloc>::size() const [with _Tp = int; _Alloc = std::__cxx11::basic_string<char>; std::vector<_Tp, _Alloc>::size_type = unsigned int]’:
main.cpp:23:34: required from here
/usr/include/c++/5/bits/stl_vector.h:655:50: error: ‘const struct std::_Vector_base<int, std::__cxx11::basic_string<char> >::_Vector_impl’ has no member named ‘_M_finish’
{ return size_type(this->_M_impl._M_finish - this->_M_impl._M_start); }
^
/usr/include/c++/5/bits/stl_vector.h:655:50: error: ‘const struct std::_Vector_base<int, std::__cxx11::basic_string<char> >::_Vector_impl’ has no member named ‘_M_start’
/usr/include/c++/5/bits/stl_vector.h: In instantiation of ‘std::_Vector_base<_Tp, _Alloc>::~_Vector_base() [with _Tp = int; _Alloc = std::__cxx11::basic_string<char>]’:
/usr/include/c++/5/bits/stl_vector.h:257:15: required from ‘std::vector<_Tp, _Alloc>::vector() [with _Tp = int; _Alloc = std::__cxx11::basic_string<char>]’
main.cpp:9:32: required from here
/usr/include/c++/5/bits/stl_vector.h:160:22: error: ‘struct std::_Vector_base<int, std::__cxx11::basic_string<char> >::_Vector_impl’ has no member named ‘_M_start’
{ _M_deallocate(this->_M_impl._M_start, this->_M_impl._M_end_of_storage
^
/usr/include/c++/5/bits/stl_vector.h:161:9: error: ‘struct std::_Vector_base<int, std::__cxx11::basic_string<char> >::_Vector_impl’ has no member named ‘_M_end_of_storage’
- this->_M_impl._M_start); }
^
/usr/include/c++/5/bits/stl_vector.h:161:9: error: ‘struct std::_Vector_base<int, std::__cxx11::basic_string<char> >::_Vector_impl’ has no member named ‘_M_start’
/usr/include/c++/5/bits/stl_vector.h:160:22: error: ‘_M_deallocate’ was not declared in this scope
{ _M_deallocate(this->_M_impl._M_start, this->_M_impl._M_end_of_storage
^
/usr/include/c++/5/bits/stl_vector.h: In instantiation of ‘std::_Vector_base<_Tp, _Alloc>::_Vector_impl::_Vector_impl() [with _Tp = int; _Alloc = std::__cxx11::basic_string<char>]’:
/usr/include/c++/5/bits/stl_vector.h:125:17: required from ‘std::_Vector_base<_Tp, _Alloc>::_Vector_base() [with _Tp = int; _Alloc = std::__cxx11::basic_string<char>]’
/usr/include/c++/5/bits/stl_vector.h:257:15: required from ‘std::vector<_Tp, _Alloc>::vector() [with _Tp = int; _Alloc = std::__cxx11::basic_string<char>]’
main.cpp:9:32: required from here
/usr/include/c++/5/bits/stl_vector.h:87:65: error: using invalid field ‘std::_Vector_base<_Tp, _Alloc>::_Vector_impl::_M_start’
: _Tp_alloc_type(), _M_start(), _M_finish(), _M_end_of_storage()
^
/usr/include/c++/5/bits/stl_vector.h:87:65: error: using invalid field ‘std::_Vector_base<_Tp, _Alloc>::_Vector_impl::_M_finish’
/usr/include/c++/5/bits/stl_vector.h:87:65: error: using invalid field ‘std::_Vector_base<_Tp, _Alloc>::_Vector_impl::_M_end_of_storage’wo ist der fehler? ich denke, der ist irgendwo beim erzeugen des Vectors. Oder bin ich da falsch auf der Spur?
-
Was soll ein std::vector<int, std::string> sein?
-
sorry, hatte da irgend ein dreher drinnen ^^ die jetzige datei:
#include <iostream> // für Eingabe, Ausgabe in der Console #include <string> // um Strings zu erstellen #include <fstream> //um Dateien zu lesen und zu schreiben #include <vector> //für vector #include <algorithm> //für std::sort using namespace std; int main(){ string s; //Variable S als String deklarieren vector<string> load; //Ein Vector deklarieren für später zum sortieren std::ifstream fil("wordlist.txt"); //einen In Stream mit einer Textdatei deklariert if(fil){ //Überprüfe ob Datei vorhanden while(fil){ //wenn Ja Schleife ausführen getline(fil,s); // Datei Zeilenweiße einlesen und in s speichern load.push_back(s); //s in vector speichern } }else{ //wenn keine Datei vorhanden, Fehlermeldung cout << "No File loaded!" << endl; } std::sort (load.begin(), load.end()); //vector sortieren for(unsigned int idx=0; idx < load.size(); ++idx){ cout << load[idx] << endl; } }
jetzt funktioniert es aber