C /C++文字列の単語を反復する最もエレガントな方法
C /C++文字列の単語を反復するエレガントな方法はありません。最も読みやすい方法は、一部の人にとっては最もエレガントであり、他の人にとっては最もパフォーマンスが高いと言えます。これを実現するために使用できる2つの方法をリストしました。最初の方法は、文字列ストリームを使用して、スペースで区切られた単語を読み取ることです。これは少し制限されていますが、適切なチェックを提供すれば、タスクはかなりうまくいきます。
例
#include <vector>
#include <string>
#include <sstream>
using namespace std;
int main() {
string str("Hello from the dark side");
string tmp; // A string to store the word on each iteration.
stringstream str_strm(str);
vector<string> words; // Create vector to hold our words
while (str_strm >> tmp) {
// Provide proper checks here for tmp like if empty
// Also strip down symbols like !, ., ?, etc.
// Finally push it.
words.push_back(tmp);
}
} もう1つの方法は、getline関数を使用して文字列を分割するカスタム区切り文字を提供することです-
例
#include <vector>
#include <string>
#include <sstream>
using namespace std;
int main() {
std::stringstream str_strm("Hello from the dark side");
std::string tmp;
vector<string> words;
char delim = ' '; // Ddefine the delimiter to split by
while (std::getline(str_strm, tmp, delim)) {
// Provide proper checks here for tmp like if empty
// Also strip down symbols like !, ., ?, etc.
// Finally push it.
words.push_back(tmp);
}
} -
Window上のc++のトップIDEは何ですか?
大きなプロジェクトは、単なるテキストエディタでは管理が困難です。このような場合にIDEを使用すると、生産性が向上し、フラストレーションが軽減される可能性があります。 IDEにはさまざまな種類があり、ニーズに合ったものを選択する必要があります。これがWindowに最適なC/C++IDEのリストです。 Visual Studio − Microsoftが開発したIDEです。このIDEは、Windows上でC ++のプログラムを構築、開発、およびプロファイリングするためのクラス最高のツールを備えています。 Visual Studioには、多数のプラグインを備えた巨大なプラグインストアもありま
-
Pythonで文字列が空かどうかを確認する最も洗練された方法は何ですか?
空の文字列は「偽」です。つまり、ブールコンテキストでは偽と見なされるため、文字列ではなく単に使用できます。 例 string = "" if not string: print "Empty String!"を出力します 出力 これにより、出力が得られます: Empty String! 例 文字列に空白を含めることができ、それでもfalseと評価したい場合は、文字列を削除してもう一度確認できます。例: string = " " if not string.strip():