
vectorの中にmapを入れて
添字:ノードID
[どのノードから来たのか|それまでのコスト]
を表現しようと考えています.
#include<iostream>
#include<vector>
#include<map>
#include<list>
// MACROS
#define UNDEF -1
// PROTOTYPE DCLARE
void init_path(std::vector<std::map<char, int> >, int size);
int main(void) {
//source -> source node
//
//size -> the number of node
//
//path -> store path infomation
// ex:
// path[1]: 1 is node id
// char : from node id
// cost : how cost from source to here
//
//adj -> show adjacency list eace node
int source;
int size;
std::vector<std::map<char, int> > path;
std::vector<std::list<char> > adj;
size = 5;
std::cout << "before" << std::endl;
init_path(path, size);
std::cout << "after" << std::endl;
std::map<char, int>::iterator it;
for(int i = 0; i < size; i++) {
it = path[i].begin();
// std::cout << it->first << ":" << it->second << std::endl;
}
return 0;
}
void init_path(std::vector<std::map<char, int> > path, int size) {
std::map<char, int> init;
init.insert( std::map<char, int>::value_type('-', UNDEF) );
for(int i = 0; i < size ; i++) {
path.push_back(init);
}
return;
}
*結果
before
after
Segmentation fault
となり初期化をする所までは正常に動いたっぽいのですが
どこが悪いのかわかりません(おそらくイテレータあたりかと思うのですが・・・・
具体的にどうしたらいいのか分からないのでご指導ねがいます.
A 回答 (2件)
- 最新から表示
- 回答順に表示
No.2
- 回答日時:
具体的に何をどうしたいのか分からないのですが、
int source;
int size = 5;
std::vector<std::map<char, int> > path(size);
std::vector<std::list<char> > adj;
//size = 5;
std::cout << "before" << std::endl;
init_path(path, size);
std::cout << "after" << std::endl;
std::map<char, int>::iterator it;
for(int i = 0; i < size; i++) {
it = path[i].begin();
std::cout << it->first << ":" << it->second << std::endl;
}
とすると、いちおう結果が出ます。
before
after
:0
:0
:0
:0
:0
No.1
- 回答日時:
void init_path(std::vector<std::map<char, int> > path, int size) {
が、
void init_path(std::vector<std::map<char, int> >& path, int size) {
の間違いでは。(初期化してないじゃん)
お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!
似たような質問が見つかりました
- C言語・C++・C# C++初心者です stirng 2 2022/09/20 20:43
- C言語・C++・C# C++プログラミングコードにポリモーフィズムを取り入れ方を教えてください。 2 2023/06/09 11:17
- C言語・C++・C# 宣言する関数の形が決まっている状態で、 str1とstr2の文字列をこの順に引っ付けてstrに保存し 2 2022/05/30 18:21
- C言語・C++・C# C++のcinの動作 5 2023/02/26 00:13
- C言語・C++・C# このプログラミング誰か教えてください 9 2022/04/22 18:50
- C言語・C++・C# c言語でユーザ関数を利用して入力された文字列を反転させるプログラムを作りたいです。 3 2023/01/29 19:47
- C言語・C++・C# c言語の問題です 課題1 (二分探索木とセット) 大きさ size の配列 array を考える。す 2 2023/01/10 21:08
- C言語・C++・C# c言語配列の結合についてです。 なぜうまくいかないのでしょうか。 #include <stdio.h 4 2022/05/30 22:42
- C言語・C++・C# C言語で再起関数とポインタを用いて文字列反転をする方法がわかりません。 4 2023/04/29 20:32
- C言語・C++・C# c言語の問題です 3 2023/01/10 16:15
関連するカテゴリからQ&Aを探す
おすすめ情報
デイリーランキングこのカテゴリの人気デイリーQ&Aランキング
-
C++、10個の数のmin&maxを求め...
-
<string.h>の関数を使わずに。
-
uniqueの使い方について
-
C言語のポインターで詰まっている
-
VS2019でofstreamが未定義になる
-
C言語からgnuplot呼び出し
-
構造体配列のvectorへの変換と...
-
STL string::findで見つからな...
-
組み合わせと順列 アルゴリズム
-
C++でShowCursorを使いたい。
-
平均値を関数を用いて出力した...
-
Haru PDF Library
-
C++で、環境変数の読み込み方を...
-
wstringの主力
-
「Aに対するBの割合」と「Aに対...
-
マイナスからプラスへ転じた時...
-
「指定されたキャストは有効で...
-
Aの値からBの値を除するとは??
-
Enterキーを押されたら次の処理...
-
2÷3などの余りについて
マンスリーランキングこのカテゴリの人気マンスリーQ&Aランキング
-
なぜ、C++の標準ヘッダをインク...
-
switch文のエラーについて
-
VC++で文字列から任意の文字を...
-
構文エラーが出ているのですが...
-
gccでコンパイル時のエラー
-
空ENTERの判別
-
vectorのイテレータを大小比較...
-
JPEGやPNGが読めるLoadImage関数
-
C++で、テキストファイルを一行...
-
std::wstringのメモリリークに...
-
enumの値から定義名を文字列化...
-
CStringとString
-
#define中の#のエスケープ
-
std::map の const 修飾について
-
#include "fstream.h"
-
_tcscat がうまくいきません(V...
-
C言語のポインターで詰まっている
-
構造体配列のvectorへの変換と...
-
C++での <iostream.h>と<iostre...
-
ヘッダーファイルがインクルー...
おすすめ情報