未分類

c++ で文字列(string)の foreach 操作を行う方法

投稿日:

string の各文字に対して操作を行うには、「iterator」(イテレータ)を使ってループを作る方法がある。

iterator の変数名は、it としておくと分かりやすい。

#include<iostream>

int main () {

std::string str1 = "good morning.";
for(std::string::iterator it = str1.begin(); it != str1.end(); ++it) {
std::cout << "*it=" << *it << std::endl;
}
return 0;
}

結果

*it=g
*it=o
*it=o
*it=d
*it=
*it=m
*it=o
*it=r
*it=n
*it=i
*it=n
*it=g
*it=.

-未分類

執筆者:


comment

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

関連記事

no image

h バー(プランク定数)の書き方

量子科学では、「プランク定数」を h で表すが、h を 2π で割った量を エイチバーで表すことがある。 tex での書き方 $\hbar$ 結果 リベンジH 4【電子書籍】[ 仙道ますみ ]価格:6 …

no image

tex でインテグラルが小さくなる→大きくする方法

文章中でインテグラル(積分記号)を大きくするには、\displaystyle と書けばよい。 例 usual $\int \frac{ax+b}{cx+d} dx$display style $\di …

no image

tex で組み合わせ記号(nCk)を入力する

tex で組み合わせ記号を入力するには、アンダーバー(_)を使う。C はローマン体にする。 例 ${}_n {\mathrm C}_k$ 結果

no image

tex の積分範囲の分数サイズを変更する方法

定積分の分数の大きさを調整するには、\mbox のサイズ指定を \small や \normalsize や \large 等に設定すると良い。 例 $\displaystyle \int _0 ^{ …

no image

tex で ξ 記号を表示する

ξ はギリシャ文字のクシー(xi)である。ゼータ(ζ)と間違えないように注意。 例 $\xi$ 結果