アルファベット文字以外のものを区切り文字とみなしたいと思っています。これどうやってするの?std :: getlineにデリミタではないものを指定できますか?
3
A
答えて
3
できません。デフォルトの区切り文字は\n
次のとおりです。他の区切り文字については
while (std::getline (std::cin, str) // '\n' is implicit
、それらを渡す:
while (std::getline (std::cin, str, ' ') // splits at a single whitespace
をただし、区切り文字はchar型のものであり、このように一つだけ「分割文字」を使用することができますが、ありません一致しないもの
入力が既にstd::string
のようなコンテナ内にある場合は、find_first_not_of
またはfind_last_not_of
を使用できます。
他の質問では、すべての回答を検討してもよろしいですか? 1つはistream::operator>>(std::istream&, <string>)
を使用します。これは一連の空白以外の文字と一致します。
3
あなたはしません。 getline
は簡単な仕事のための簡単なツールです。より複雑なものが必要な場合は、RegExなどのより複雑なツールを使用する必要があります。
0
あなたはstd::getline()
を使用してあなたが望むことはできませんが、自分でロールすることはできます。ここでは、文字がデリミタであるかどうかを示す述語(関数、ファンクタ、ラムダ、C++ 11の場合)を指定して、デリミタ文字列を渡すためのいくつかのオーバーロードを示すようなバリアント( strtok()
):
#include <functional>
#include <iostream>
#include <string>
using namespace std;
template <typename Predicate>
istream& getline_until(istream& is, string& str, Predicate pred)
{
bool changed = false;
istream::sentry k(is,true);
if (bool(k)) {
streambuf& rdbuf(*is.rdbuf());
str.erase();
istream::traits_type::int_type ch = rdbuf.sgetc(); // get next char, but don't move stream position
for (;;ch = rdbuf.sgetc()) {
if (istream::traits_type::eof() == ch) {
is.setstate(ios_base::eofbit);
break;
}
changed = true;
rdbuf.sbumpc(); // move stream position to consume char
if (pred(istream::traits_type::to_char_type(ch))) {
break;
}
str.append(1,istream::traits_type::to_char_type(ch));
if (str.size() == str.max_size()) {
is.setstate(ios_base::failbit);
break;
}
}
if (!changed) {
is.setstate(ios_base::failbit);
}
}
return is;
}
// a couple of overloads (along with a predicate) that allow you
// to pass in a string that contains a set of delimiter characters
struct in_delim_set : unary_function<char,bool>
{
in_delim_set(char const* delim_set) : delims(delim_set) {};
in_delim_set(string const& delim_set) : delims(delim_set) {};
bool operator()(char ch) {
return (delims.find(ch) != string::npos);
};
private:
string delims;
};
istream& getline_until(istream& is, string& str, char const* delim_set)
{
return getline_until(is, str, in_delim_set(delim_set));
}
istream& getline_until(istream& is, string& str, string const& delim_set)
{
return getline_until(is, str, in_delim_set(delim_set));
}
// a simple example predicate functor
struct is_digit : unary_function<char,bool>
{
public:
bool operator()(char c) const {
return ('0' <= c) && (c <= '9');
}
};
int main(int argc, char* argv[]) {
string test;
// treat anything that's not a digit as end-of-line
while (getline_until(cin, test, not1(is_digit()))) {
cout << test << endl;
}
return 0;
}
関連する問題
- 1. スカラとは何ですか?getline()、std :: cin.eof()、std :: cin.bad()?ここ
- 2. を選択し、STD :: CINとstd :: getlineのはよく一緒に遊んでいない
- 3. std :: getlineはstd :: back_insert_iteratorを受け入れません
- 4. std :: getlineは空白を削除しますか?
- 5. はistringstream(C++)からではないのgetline()は
- 6. split()でもデリミタを保持
- 7. インタフェースはメソッド名のみを指定できますが、戻り値の型は指定できませんか?
- 8. seekgがgetlineで動作しないのはなぜですか?
- 9. いつもstd :: forwardを使うのはなぜですか?
- 10. C++ std :: getline結果文字列で別の文字列を連結することはできません
- 11. std :: getline関数を正しく使用するには
- 12. jqGrid行をオプションとして指定しても編集できないのはなぜですか?
- 13. std :: reverse_iterator :: operator []の戻り値の型が指定されていないのはなぜですか?
- 14. フォールバックフォントごとに異なるフォントサイズを指定できますか?
- 15. CSSの特定の行にスタイリングを指定できますか?
- 16. std :: functionのデフォルト値を指定する
- 17. 純粋なSVG文書でFavicon.icoを指定できますか?
- 18. なぜ、__cache_hash_codeは、std :: __ unordered_mapにのみ使用できますか?
- 19. なぜstd :: map.begin()+ 1を実行できないのですか?
- 20. 特定のファイルを分析しないように指示できますか?
- 21. C++標準でstd :: bitset :: referenceのデストラクタが指定されているのはなぜですか?
- 22. なぜ次のものをコンパイルできないのですか?
- 23. C++のstd :: weak_ptrにstd :: hashが定義されていないのはなぜですか?
- 24. getlineをスキップするgetline
- 25. `std :: unique_ptr`のコンテナで` std :: remove_if`を使用できますか?
- 26. なぜKotlinでvar/valループを指定できないのですか?
- 27. OpenGraph:FB.apiでどのようにフィルタを指定できますか?
- 28. ロックに関するstd :: atomicの実装は、プログラムの動作を正しいものから正しくないものに変更できますか?
- 29. GDBでブレークポイントを指定することはできますか?
- 30. ディレクトリパスをワイルドカードで指定することはできますか?