よくありますか?文字列とはstd::stringを意味します
10 に答える
私が使用するperlスタイルの分割関数は次のとおりです。
void split(const string& str, const string& delimiters , vector<string>& tokens)
{
// Skip delimiters at beginning.
string::size_type lastPos = str.find_first_not_of(delimiters, 0);
// Find first "non-delimiter".
string::size_type pos = str.find_first_of(delimiters, lastPos);
while (string::npos != pos || string::npos != lastPos)
{
// Found a token, add it to the vector.
tokens.push_back(str.substr(lastPos, pos - lastPos));
// Skip delimiters. Note the "not_of"
lastPos = str.find_first_not_of(delimiters, pos);
// Find next "non-delimiter"
pos = str.find_first_of(delimiters, lastPos);
}
}
C ++で文字列を分割する組み込みの方法はありませんが、boostは、文字列分割を含むあらゆる種類の文字列操作を行うための文字列アルゴリズムライブラリを提供します。
STL文字列
文字列イテレータを使用して、汚い作業を行うことができます。
std::string str = "hello world";
std::string::const_iterator pos = std::find(string.begin(), string.end(), ' '); // Split at ' '.
std::string left(str.begin(), pos);
std::string right(pos + 1, str.end());
// Echoes "hello|world".
std::cout << left << "|" << right << std::endl;
void split(string StringToSplit, string Separators)
{
size_t EndPart1 = StringToSplit.find_first_of(Separators)
string Part1 = StringToSplit.substr(0, EndPart1);
string Part2 = StringToSplit.substr(EndPart1 + 1);
}
答えはいいえだ。ライブラリ関数の1つを使用してそれらを分割する必要があります。
私が使用するもの:
std::vector<std::string> parse(std::string l, char delim)
{
std::replace(l.begin(), l.end(), delim, ' ');
std::istringstream stm(l);
std::vector<std::string> tokens;
for (;;) {
std::string word;
if (!(stm >> word)) break;
tokens.push_back(word);
}
return tokens;
}
メソッドを見てbasic_streambuf<T>::underflow()、フィルターを作成することもできます。
これを行う一般的な方法はありません。
私は、ヘッダーのみで使いやすいboost::tokenizerを好みます。
一体何だ...これが私のバージョンだ...
注: ("XZaaaXZ", "XZ") で分割すると、3 つの文字列が得られます。TheIncludeEmptyStrings が false の場合、これらの文字列のうち 2 つが空になり、 theStringVector に追加されません。
Delimiter はセット内の要素ではありませんが、正確にその文字列に一致します。
inline void
StringSplit( vector<string> * theStringVector, /* Altered/returned value */
const string & theString,
const string & theDelimiter,
bool theIncludeEmptyStrings = false )
{
UASSERT( theStringVector, !=, (vector<string> *) NULL );
UASSERT( theDelimiter.size(), >, 0 );
size_t start = 0, end = 0, length = 0;
while ( end != string::npos )
{
end = theString.find( theDelimiter, start );
// If at end, use length=maxLength. Else use length=end-start.
length = (end == string::npos) ? string::npos : end - start;
if ( theIncludeEmptyStrings
|| ( ( length > 0 ) /* At end, end == length == string::npos */
&& ( start < theString.size() ) ) )
theStringVector -> push_back( theString.substr( start, length ) );
// If at end, use start=maxSize. Else use start=end+delimiter.
start = ( ( end > (string::npos - theDelimiter.size()) )
? string::npos : end + theDelimiter.size() );
}
}
inline vector<string>
StringSplit( const string & theString,
const string & theDelimiter,
bool theIncludeEmptyStrings = false )
{
vector<string> v;
StringSplit( & v, theString, theDelimiter, theIncludeEmptyStrings );
return v;
}
C文字列
\0分割したい場所を挿入するだけです。これは、標準のC関数で取得できるのとほぼ同じくらい組み込みです。
この関数は、charセパレーターの最初の出現時に分割され、2番目の文字列を返します。
char *split_string(char *str, char separator) {
char *second = strchr(str, separator);
if(second == NULL)
return NULL;
*second = '\0';
++second;
return second;
}
かなり単純なメソッドは、std :: stringのc_str()メソッドを使用してCスタイルの文字配列を取得し、次にstrtok()を使用して文字列をトークン化することです。ここにリストされている他のソリューションほど雄弁ではありませんが、簡単で機能します。