質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
C++

C++はC言語をもとにしてつくられた最もよく使われるマルチパラダイムプログラミング言語の1つです。オブジェクト指向、ジェネリック、命令型など広く対応しており、多目的に使用されています。

Q&A

解決済

2回答

1867閲覧

cinでvector配列代入

退会済みユーザー

退会済みユーザー

総合スコア0

C++

C++はC言語をもとにしてつくられた最もよく使われるマルチパラダイムプログラミング言語の1つです。オブジェクト指向、ジェネリック、命令型など広く対応しており、多目的に使用されています。

0グッド

0クリップ

投稿2019/02/23 07:39

編集2019/02/24 05:05

cinとofsのところでエラーが出ています。

エラー(一部)

no operator ">>" matches these operands -- operand types are: std::basic_istream<char, std::char_traits<char>> >> <unknown-type>
no operator "<<" matches these operands -- operand types are: std::ofstream << std::vector<std::string, std::allocatorstd::string>

c++

1#ifndef _MCR_data 2#define _MCR_data 3 4#include <iostream> 5#include <fstream> 6#include <vector> 7#include <string> 8 9 10using std::cout; 11using std::cin; 12using std::string; 13using std::ofstream; 14using std::endl; 15using std::vector; 16using std::string; 17 18 19const int x=100; 20extern vector<string> strategy_data;//[]付けない 21extern vector<string> cause_data; 22extern vector<string> reflection_data; 23 24 25class MCR{ 26 27 string strategy_data_source; 28 string cause_data_source; 29 string reflection_data_source; 30 public: 31 void MCR_file_put(int flag); 32 void MCR_file_delete(); 33}; 34 35void MCR::MCR_file_put(int flag){ 36 if(flag == 1){ 37 cout << "MCR_strategy.txt in wite" << endl; 38 ofstream ofs("MCR_strategy.txt",std::ios::app); 39 cin >> strategy_data_source >> endl; 40 strategy_data.push_back(strategy_data_source); 41 ofs << strategy_data << endl; 42 43 } 44 if(flag == 2){ 45 cout << "MCR_cause.txt in wite" << endl; 46 ofstream ofs("MCR_cause.txt",std::ios::app); 47 cin >> cause_data_source >> endl; 48 cause_data.push_back(cause_data_source); 49 ofs << cause_data << endl; 50 51 } 52 if(flag == 3){ 53 cout << "MCR_reflection.txt in wite" << endl; 54 ofstream ofs("MCR_reflection.txt",std::ios::app); 55 cin >> reflection_data_source >> endl; 56 reflection_data.push_back(reflection_data_source); 57 ofs << reflection_data << endl; 58 59 } 60 61} 62 63 64 65 66 67#endif

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答2

0

C++

1#include <vector> 2#include <iterator> 3#include <iostream> 4 5int main() { 6 using namespace std; 7 8 vector<int> vec{istream_iterator<int>(cin), istream_iterator<int>()}; 9 10 // デキタカナ? 11 for ( auto item : vec) { 12 cout << item << endl; 13 } 14 15} 16 17/* 実行結果 181 2 3 4 5 6 ^Z      ←入力 191 202 213 224 235 246 25*/

投稿2019/02/23 18:00

episteme

総合スコア16614

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

退会済みユーザー

退会済みユーザー

2019/02/24 04:50

申し訳ありませんが、istream_iterator<int>(cin), istream_iterator<int>()の部分の宣言意味が分かりません。どういったものか、教えてほしいです。簡単なvectorの宣言しかしたことがなくて、、、。
退会済みユーザー

退会済みユーザー

2019/02/25 13:02

リファレンス読んでみましたがちょっと難しくて理解できませんでした。 istream_iterator<int>(cin), istream_iterator<int>()を用いた理由としては、cinで代入した回数をvecにいれたいからですか?
episteme

2019/02/25 18:39

えーと...わからんなら手慣れたやり方でどうぞ。
guest

0

ベストアンサー

ちょっと見ですが、
extern vector<string> strategy_data[];
extern vector<string> cause_data[];
extern vector<string> reflection_data[];
は、
extern vector<string> strategy_data;
extern vector<string> cause_data;
extern vector<string> reflection_data];
の間違いでは?
上記ではvectorの配列になってしまいます。

あと、“using namespace std;”は何処に?
なければ、extern std::vector<string>

で、std::cinはvectorに直接データを書き込めません。
なので、一旦std::stringに読み込んでpush_back()で挿入。
[追記]
ちなみにテスト用のソース

cpp

1usr ~/Project/test % cat tst.cpp 2 3#include <iostream> 4#include <string> 5#include <vector> 6 7int main(void) 8{ 9 std::vector<std::string> sv; 10 std::string s; 11 12 std::cin >> s ; 13 14 sv.push_back(s); 15 16 std::cout << sv[0] << std::endl; 17 18 return 0; 19} 20

コンパイルと結果です。
usr ~/Project/test % c++ tst.cpp
usr ~/Project/test % ./a.out
123456789abcdef
123456789abcdef
usr ~/Project/test %

コンパイラとオプションです。

text

1alias c++ /xxxxx/bin/clang++ -Wno-c++98-compat -Wno-padded -pipe -std=c++17 -Weverything -Ofast 2usr ~/Project/test % c++ --version 3clang version 8.0.0 (trunk 350063) 4Target: x86_64-unknown-linux-gnu 5Thread model: posix 6InstalledDir: /xxxxx/bin 7usr ~/Project/test % 8

投稿2019/02/23 07:55

編集2019/02/24 11:24
cateye

総合スコア6851

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

退会済みユーザー

退会済みユーザー

2019/02/24 04:56 編集

僕は、using std::cout; using std::cin; using std::string; using std::ofstream; using std::endl; using std::vector; こうやって使用する分だけ宣言しておいてあるので、using namespace::std;は使っておりません。 cinから直接代入できないんですね。cateyeさんのおっしゃる通り、stringに読み込んでpush_back()で挿入しようと思います。
退会済みユーザー

退会済みユーザー

2019/02/24 05:07

上記のやり方にしてやってみたところ、string型に代入しようと試みたところno operator ">>" matches these operands -- operand types are: std::basic_istream<char, std::char_traits<char>> >> <unknown-type>といったエラーが出てきました。
退会済みユーザー

退会済みユーザー

2019/02/25 12:56

なんとかコンパイルできましたありがとうございました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問