std::string splitten an Whitspaces
-
auf Stackoverflow habe ich gefunden:
#include <boost/algorithm/string.hpp> std::vector<std::string> strs; boost::split(strs, "string to split", boost::is_any_of("\t "));
Kann man das
boost::is_any_of("\t ")
noch verbessern, dass es auch Zeilenumbrüche beinhaltet mittels http://www.cplusplus.com/reference/cctype/isspace/?
-
Das hier funktioniert leider nicht:
#include "stdafx.h" #include <iostream> #include <boost/algorithm/string.hpp> #include <vector> #include <cctype> using namespace std; using namespace boost; int main(void) { string input; cout << "String eingeben: "; cin >> input; vector<string> splitted_string; split(splitted_string, input, std::isspace); cout << "Der String hat " << splitted_string.size() << " Wörter"; char c; cin >> c; return 0; }
Wenn ich da "Hallo Hallo Hallo" eingebe, kommt: "Der String hat 1 Wörter".
-
Außerdem gibt mir die Version mit boost::is_any_of folgenden Compilerfehler (VC++ 2015):
1>c:\program files (x86)\microsoft visual studio 14.0\vc\include\xutility(2372): error C4996: 'std::copy::_Unchecked_iterators::_Deprecate': Call to 'std::copy' with parameters that may be unsafe - this call relies on the caller to check that the passed values are correct. To disable this warning, use -D_SCL_SECURE_NO_WARNINGS. See documentation on how to use Visual C++ 'Checked Iterators' 1> c:\program files (x86)\microsoft visual studio 14.0\vc\include\xutility(2372): note: Siehe Deklaration von "std::copy::_Unchecked_iterators::_Deprecate"
obwohl ich
-D_SCL_SECURE_NO_WARNINGS
in den Präprozessordefinitionen habe.
-
Nach http://stackoverflow.com/questions/5117201/multiple-split-tokens-using-boostis-any-of
würde ich für Tabs und Zeilenumbrüche:
std::vector<std::string> strs; boost::split(strs, "string to split", boost::is_any_of("\t\n"));
versuchen
-
dsfdsd schrieb:
Das hier funktioniert leider nicht:
int main(void) { string input; cin >> input; }
Wenn ich da "Hallo Hallo Hallo" eingebe, kommt: "Der String hat 1 Wörter".
Klar, denn mit >> liest du automatisch nur bis zum Whitespace ein. Input hat also den Wert "Hallo". Darin gibt es nichts zu splitten.
Wenn du die gesamte Zeile lesen willst, dann:
std::string input; std::getline(std::cin, input);
-
wob schrieb:
dsfdsd schrieb:
Das hier funktioniert leider nicht:
int main(void) { string input; cin >> input; }
Wenn ich da "Hallo Hallo Hallo" eingebe, kommt: "Der String hat 1 Wörter".
Klar, denn mit >> liest du automatisch nur bis zum Whitespace ein. Input hat also den Wert "Hallo". Darin gibt es nichts zu splitten.
Wenn du die gesamte Zeile lesen willst, dann:
std::string input; std::getline(std::cin, input);
Danke, das funktioniert jetzt.
Nächstes Problem: Wenn ich mehrere Whitespaces hintereinander eingebe, produziert mir boost::split leere Strings. Wie kann ich das fixen?
-
token_compress_on
-
Danke, das funktioniert!