##問題文
文字列s と整数i(1≦i≦|s|) が与えられます。 sのi文字目を出力してください。
なお、|s|は文字列 sの長さを表します。
###制約
1≦|s|≦100 sは英小文字のみからなる。
1≦i≦|s|
##入力
入力は以下の形式で標準入力から与えられる。
s
i
##出力
sのi文字目を出力せよ。
という問題で自分は
#include <bits/stdc++.h> using namespace std; int main() { string s; int i; cin >> s >> i; for (i = 0;i < s;i++) { cout << s.at(i) << endl; } }
とコンパイルを試みたところ
./Main.cpp: In function ‘int main()’: ./Main.cpp:9:16: error: no match for ‘operator<’ (operand types are ‘int’ and ‘std::string {aka std::basic_string<char>}’) for (i = 0;i < s;i++) { ^ In file included from /usr/include/c++/5/utility:70:0, from /usr/include/c++/5/algorithm:60, from /usr/include/x86_64-linux-gnu/c++/5/bits/stdc++.h:64, from ./Main.cpp:1: /usr/include/c++/5/bits/stl_pair.h:220:5: note: candidate: template<class _T1, class _T2> bool std::operator<(const std::pair<_T1, _T2>&, const std::pair<_T1, _T2>&) operator<(const pair<_T1, _T2>& __x, const pair<_T1, _T2>& __y) ^ /usr/include/c++/5/bits/stl_pair.h:220:5: note: template argument deduction/substitution failed: ./Main.cpp:9:18: note: mismatched types ‘const std::pair<_T1, _T2>’ and ‘int’ for (i = 0;i < s;i++) { ^ In file included from /usr/include/c++/5/bits/stl_algobase.h:67:0, from /usr/include/c++/5/algorithm:61, ...
となってしまいました。
##試したこと
cout << s.at(i) << endl;
をfor文から出してみましたが同じようなエラーが出てしまいました。エラー内容的に少しやったことがある方でしたらまたあり得ないようなプログラムを作成してると思いますが、何卒よろしくお願いします。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2020/02/25 21:38 編集
2020/02/26 05:21