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

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

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

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

Q&A

解決済

1回答

1642閲覧

コンテナクラスで "std::vector<std::map<std::string,std::vector<std::string>>>" の場合どうすれば中を参照できるのか知りたい

退会済みユーザー

退会済みユーザー

総合スコア0

C++

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

0グッド

0クリップ

投稿2020/12/02 08:33

どうすればコメント部の内部 // ------ のitr-> ですがどうすれば std::map<string std::vector<string>>の中を参照してitr->second といったように触れるのでしょうか?

cpp

1#include <iostream> 2 3#include <vector> 4#include <map> 5 6#include <string> 7#include <stdlib.h> 8#include <compare> 9#include <filesystem> 10 11int main() 12{ 13 //_popen("notepad", "rb"); 14 std::string FilePath = "C:\Users\yw325\Desktop\Test\Test\Music_Data"; 15 16 std::map<std::string, std::vector<std::string>> idx; 17 std::vector< std::map<std::string, std::vector<std::string>> > mIndex; // ライブラリ全体 18 19 std::string name = ""; 20 21 22 for (std::filesystem::directory_entry itr : std::filesystem::recursive_directory_iterator(FilePath)) 23 { 24 if (itr.is_directory() == true) 25 { 26 //ディレクトリの場合 27 name = std::filesystem::path(itr.path().filename()).string(); //ディレクトリ名をキーに挿入 28 //name = std::filesystem::absolute(std::filesystem::path(itr.path().filename())).string(); 29 } 30 else 31 { 32 //フォルダの場合 33 idx[name].push_back(std::filesystem::absolute(itr.path()).string()); //フォルダの絶対パスを挿入 34 mIndex.push_back(idx); 35 } 36 } 37 38 39 // ----------------------------------------------------------------------------------------------------------------------------------- 40 for (std::vector<std::map<std::string, std::vector<std::string>>>::iterator itr = mIndex.begin(); itr != mIndex.end(); itr++) 41 { 42 43 std::cout << "key: " << itr << std::endl; 44 45 46 for (std::vector<std::string>::iterator it = itr->second.begin(); it != itr->second.end(); it++) 47 { 48 printf(" %s\n", it->c_str()); 49 } 50 51 52 53 } 54 // ----------------------------------------------------------------------------------------------------------------------------------- 55 return 0; 56} 57

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

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

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

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

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

guest

回答1

0

ベストアンサー

iteratorが指し示す先が何型になるのかを落ち着いて考えれば良いかと.

C++

1int main(void) 2{ 3 using namespace std; 4 vector< map< string, vector<string> > > Data(2); 5 Data[0][ "Cat" ] = std::vector<string>{ "Toyger", "Cheetoh" }; 6 Data[0][ "Book" ] = std::vector<string>{ "BORN SURVIVOR", "Effective C++", "ALGO" }; 7 Data[1][ "No" ] = std::vector<string>{ "3", "9", "0", "8", "3", "1" }; 8 9 for( auto ivec=Data.begin(); ivec!=Data.end(); ++ivec ) 10 { //ivecが指すのはDataの要素 : map< string, vector<string> > 型 11 cout << "---map---\n"; 12 for( auto imap=ivec->begin(); imap!=ivec->end(); ++imap ) 13 { //imapが指すのはmapの要素 : pair< string, vector<string> > 型 14 cout << "Key = " << imap->first << endl; 15 for( const auto &str : imap->second ){ cout << " " << str << endl; } 16 } 17 } 18 19 return 0; 20}

投稿2020/12/02 08:59

fana

総合スコア11658

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

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

退会済みユーザー

退会済みユーザー

2020/12/02 12:57

質問ですがmIndexをmIndex.at(0)などといったように変数に入れる場合どうすればいいのでしょうか?
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問