回答編集履歴
3
文章の不備修正
answer
CHANGED
@@ -1,8 +1,10 @@
|
|
1
1
|
tyapapaさん
|
2
2
|
|
3
|
-
std::vector<char*>の要素に
|
3
|
+
std::vector<char*>の要素に
|
4
|
+
const char*型を格納しているのと、
|
4
|
-
std::cout << vstr << std::endl;の部分で
|
5
|
+
std::cout << vstr << std::endl;の部分で
|
6
|
+
vectorオブジェクトをそのまま
|
5
|
-
|
7
|
+
表示しようとしているのがエラーの原因です。
|
6
8
|
|
7
9
|
下記に修正したコード載せておきます。
|
8
10
|
|
2
wandboxのリンク追加
answer
CHANGED
@@ -41,4 +41,5 @@
|
|
41
41
|
for (int i=0; i<5; i++) vstr.push_back(pstr);// 5回vstrへ追加
|
42
42
|
std::cout << vstr << std::endl;// 表示
|
43
43
|
}
|
44
|
-
```
|
44
|
+
```
|
45
|
+
[wandboxで実行](https://wandbox.org/permlink/8aM5F8l2dNDCWiHX)
|
1
<<演算子のオーバーロードについて追記
answer
CHANGED
@@ -18,4 +18,27 @@
|
|
18
18
|
for(const auto& i:vstr)std::cout << i << std::endl;
|
19
19
|
}
|
20
20
|
```
|
21
|
-
[wandboxで実行](https://wandbox.org/permlink/fCoDZSew4Afgv8IZ)
|
21
|
+
[wandboxで実行](https://wandbox.org/permlink/fCoDZSew4Afgv8IZ)
|
22
|
+
|
23
|
+
追記
|
24
|
+
std::vectorはstd::coutで直接内容を表示できないですが
|
25
|
+
下記のように自分で<<演算子のオーバーロードを作れば
|
26
|
+
std::cout << std::vectorのオブジェクト << std::endl;
|
27
|
+
で表示できるようになります。
|
28
|
+
```c++
|
29
|
+
#include <iostream>
|
30
|
+
#include <vector>
|
31
|
+
|
32
|
+
std::ostream& operator<<(std::ostream& os, const std::vector<const char*>& vec){
|
33
|
+
for(const auto& i:vec) os << i << std::endl;
|
34
|
+
return os;
|
35
|
+
}
|
36
|
+
|
37
|
+
int main(){
|
38
|
+
const char* pstr = "abcdef";// 配列
|
39
|
+
std::vector<const char*> vstr;// ベクター配列
|
40
|
+
|
41
|
+
for (int i=0; i<5; i++) vstr.push_back(pstr);// 5回vstrへ追加
|
42
|
+
std::cout << vstr << std::endl;// 表示
|
43
|
+
}
|
44
|
+
```
|