未分類

c++ で文字列が他の文字列を含むか判定する方法

投稿日:

文字列を含むかどうか判定するには、string の find を使う。

見つからなかった場合は、std::string::npos を返す。

#include <iostream>
#include <string>

using namespace std;
int main ()
{
string str1 = "おはようございます";
string str2 = "よう";

int found = str1.find(str2);

if (found != std::string::npos) {
std::cout << "位置: " << int(found) << std::endl;
}
return 0;
}

結果

位置: 6

-未分類

執筆者:


comment

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です

関連記事

no image

tex でオーム記号を入力する方法

siunit のパッケージを使って、\si{\ohm} で入力する。 例 \usepackage{siunitx} \begin{document} \si{\ohm} \end{document} …

no image

tex で小文字のL(リットル)記号を入力する方法

tex で小文字のL(l)リットル記号を入力するには、\ell を使う。通常の小文字の l は、\\l であるのに注意。 例 5 \\ell 結果

no image

tex 大なり・小なり記号

tex で、「大なり」「小なり」を入力する方法通常の「大なり」「小なり」記号 例 $>$ $<$ 結果 参考 不等号を含むもの(大なりイコール・小なりイコール)を入力することもできる。イコールは …

no image

matplotlib で x軸・y軸ラベルをつける

plt.xlabel と plt.ylabel を使う。 例 import numpy as np import matplotlib.pyplot as plt x = np.linspace(0, …

no image

tex で文字を上付きにした矢印を入力する方法

\overset を使う方法がある。 例 $\overset{\text{abc}}{\rightarrow}$ 結果