LoginSignup
74
66

More than 5 years have passed since last update.

競プロ等におけるC++の標準入力

Last updated at Posted at 2017-10-28

あまり使っていないので、もっと良い書き方があるかもしれません。


入力:文字数の指定がない場合

1 2 3 4 5
1
2
3
4
5

コード

#include <iostream>
using namespace std;
int main(void){
    int l, i=0, a[10];
    while(cin>>l){
        a[i] = l;
        cout << a[i];
        i++;
    }
}

出力

1 2 3 4 5

入力:文字列かつスペースで分割したくない場合

a b c d efg

コード

#include <iostream>
#include <string.h>
using namespace std;
int main() {
    string s;
    getline(cin,s);
    cout << s << "\n";
    cout << s[0];
}

出力

a b c d efg
a

入力:カンマ区切りの複数行複数列

1,2,3
4,5,6
7,8,9
0,1,2

コード

#include <iostream>
#include <string.h>
using namespace std;
int main() {
    int i;
    char str[10], *a[10],*b[10],*c[10];
    while(cin >> str){           //行数指定がある場合はif文でも可
       a[i] = strtok(str, ",");  //","で分解
       b[i] = strtok(NULL, ",");
       c[i] = strtok(NULL, ",");
        cout << a[i] << b[i] << c[i]  <<"\n";
        i++;
    }
    return 0;
}

出力

123
456
789
012

久々に使うとかなり混乱しますね...

入力:行数、列数指定の無い入力

1 2 3 4
5 6 7 8
9 0 1 2

に対し、a[0][0]=1, a[1][0]=2 となるような行列風配列の方法を考えていますが、どうするのが一番楽だろうか

74
66
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
74
66