c++ Bus Error (core dumped) - c++

I successfully compiled following codes, but when I tried to run the codes, a "Bus error (core dumped)" occurred every time I finished my first input of "cin >> instruct >> name >> Bal". I searched online about the bus error, but I still couldn't find my error. Please help me with this, thanks a lot !!
// Bank.h
1 #ifndef BANK_H
2 #define BANK_H
3 using namespace std;
4
5 class BankAccount{
6 private:
7 string _name;
8 double _balance;
9
10 public:
11 BankAccount(string, double);
12 string getName();
13 void setName(string);
14 double getBalance();
15 void setBalance(double);
16 void Withdraw(double);
17 void Deposite(double);
18 void interest(int, int);
19
20 };
21 #endif
//Bank.cpp
1 #include<iostream>
2 #include<string>
3 #include "Bank.h"
4 using namespace std;
5
6 BankAccount::BankAccount(string name, double balance):_name(name),
7 _balance(balance){}
8
9 string BankAccount::getName(){ return _name;}
10
11 double BankAccount::getBalance(){ return _balance;}
12
13 void BankAccount::setName(string name){
14 _name = name;
15 return;
16 }
17
18 void BankAccount::setBalance(double balance){
19 _balance = balance;
20 return;
21 }
22
23 void BankAccount::Withdraw(double balance)
24 {
25
26 _balance = _balance - balance;
27 return;
28 }
29
30 void BankAccount::Deposite(double balance)
31 {
32
33 _balance = _balance + balance;
34 return;
35 }
36
37 void BankAccount::interest(int interestRate, int M)
38 {
39 double interest;
40
41 interest = _balance*(interestRate/1200*1.0)*M;
42 _balance = _balance + interest;
43
44 return;
45 }
//BankMain.cpp
1 #include<iostream>
2 #include<string>
3 #include "Bank.h"
4 using namespace std;
5
6 int main()
7 {
8 int x, p, check=1, i=0, j;
9 double Bal;
10 BankAccount* Account[100];
11 string name;
12 string instruct;
13
14 cin >> x >> p;
15
16 while(check)
17 {
18 cin >> instruct >> name >> Bal;
19
20 if(instruct == "Create")
21 {
22 Account[i]->setName(name);
23 Account[i]->setBalance(Bal);
24 Account[i]->interest(x, p);
25 i++;
26 }
27 else
28 {
29 if(instruct == "Withdraw")
30 {
31 for(j=0; j<i;j++)
32 {
33 if(Account[j]->getName() == name)
34 break;
35 }
36 Account[j]->Withdraw(Bal);
37
38 }
39
40 if(instruct == "Deposite")
41 {
42 for(j=0; j<i; j++)
43 {
44 if(Account[j]->getName() == name)
45 break;
46 }
47 Account[j]->Deposite(Bal);
48 }
49 }
50
51 if(instruct == "0")
52 check = 0;
53 }
54
55 cout << i;
56 for(j=0; j<i; j++)
57 {
58 cout << Account[j]->getName() << " " << Account[j]->getBalance();
59 cout << endl;
60 }
61
62 return 0;
63 }

You have many pitfalls in your code.
The first one is that you defined an array of pointers BankAccount* Account[100]; and you access it as they are initialized... Why? This array contains junk and if you need to create a new account use new BankAccount(name, balance) first and assign it to the appropriate index in this array.
Then all your internal loops which scan for a specific name assume that this name is found and access Account[j]->... but what if j==i and this name was not found?
These are the main ones that I saw.
Of course there are more but they should not cause "core dump".
Like passing string by value or dividing integer by 1200 (if that integer is smaller than 1200 you will get 0).

Related

reading for value in file giving wrong results - c++

posting again due to change in formatting and code
I'm trying to parse a csv file and search it to find values that match my randomly generated array.
I've tried a few methods but im getting lost, I need to access the entire file, search for these values and output the index column that it belongs to.
my code for my random array generator as well as my search csv function:
reproducible example:
#include <fstream>
#include <iostream>
#include <string>
#include <random>
using namespace std;
//fills array of size 6 with random numbers between 1-50
void random_array(int arr[])
{
for (int i = 0; i < 6; i++) {
int num = rand() % 51;
arr[i] = num;
}
}
int main()
{
string line;
fstream file;
int size = 6;
int numbers[size];
int found;
//gets random array from random_array func
srand(time(NULL));
random_array(numbers);
//opens file to search
file.open("CSV directory here");
if (file.is_open()) {
//print random array values
for (int i = 0; i < size; i++) {
cout << numbers[i] << " ";
}
cout << endl;
//get file contents and search if contents of array exist in file
while (getline(file, line)) {
for (int i = 0; i < size; i++) {
found = line.find(std::to_string(numbers[i]));
if (found != string::npos) {
//print found numbers
cout << found << " ";
}
}
}
}
return 0;
}
CSV sample format:
,Date,1,2,3,4,5,6,7
0,Wed 03 January 2001,5,6,16,17,19,22,40
1,Sat 06 January 2001,6,16,23,34,35,40,37
my output:
array generated:
35 35 38 37 16 31
numbers found (from entire csv not just sample above):
26 33 33 39 24 31 34 33 24 6 28 31 33 33 30 0 32 34 33 30 27 38 26 29
29 33 7 24 29 26 26 26 24 24 0 30 30 36 0 0 23 27 0 0 7 36 36 27 30 30
27 27 26 26 32 29 23 38 32 32 28 28 7 25 29 37 25 29 29 26 23 34 31 28
25 31 28 28 34 32 32 35 38 40 25 37 37 35 40 40 30 30 42 42 42 36 35
28 31 25 37 7 27 36 33 36 33 29 39 29 35 34 34 40 40 43 31
Obviously you can see these numbers aren't correct and are almost completely random. The numbers dont match what's within my array, am I doing something wrong with my file parsing?

How did changing datatype of an array from int to long long and INT_MAX to LLONG_MAX in a C++ code result in a runtime error?

The values in my input testcase files were such that at some point in the code, values would exceed the capacity of int, so I figured I'd change the datatype of this particular array holding this value greater than INT_MAX from int to long long and change maximum values in the code to LLONG_MAX from INT_MAX so that comparisons during runtime don't yield a wrong answer.
However, now the code seems to get stuck with a runtime error even before arriving at the mentioned testcase. It now fails at a case that it used to pass when the values were all int oriented. I don't understand how this is possible.
The testcase which passes with int but fails with ll is:
100 50
1 23 133
1 87 16
2 9 78
3 12 117
3 39 19
5 25 219
5 47 130
5 97 157
6 50 114
9 11 25
9 39 227
10 45 187
10 77 120
12 19 85
13 43 247
14 16 4
15 33 223
16 33 1
19 69 204
20 35 119
20 43 213
20 86 19
22 40 233
23 33 61
23 79 152
26 89 213
27 57 129
28 42 220
31 68 84
31 69 183
32 39 145
32 100 117
33 49 198
34 48 78
37 66 200
37 91 77
39 44 235
41 70 109
42 92 33
44 74 196
48 73 26
51 57 216
53 70 158
63 98 220
66 72 148
80 93 150
81 99 54
83 84 129
83 89 177
95 100 16
Below is the code that gives an error at this tc.
#include<bits/stdc++.h>
using namespace std;
# define ll long long int
ll update, previous;
set<pair<ll, int>> dist;
auto it=dist.begin();
int ind=0, n, i, j;
pair<ll, int>p;
void dij(vector<pair<int, ll>> tree[], bool decided[], ll d[], int path[]) {
ind=0;
while(!dist.empty()) {
it=dist.begin();
if(it==dist.end()) return;
ind=it->second;
dist.erase(it);
decided[ind]=1;
for(j=0; j<tree[ind].size(); j++) {
update=d[ind]+tree[ind][j].second;
previous=d[tree[ind][j].first];
if(update<previous) {
p=make_pair(previous, tree[ind][j].first);
dist.erase(dist.find(p));
p=make_pair(update, tree[ind][j].first);
dist.insert(p);
path[tree[ind][j].first]=ind;
}
d[tree[ind][j].first]=min(update, previous);
}
}
}
int main()
{
ll edges;
ll x, y, weight;
cin>>n>>edges;
vector<pair<int, ll>> graph[n];
for(i=0; i<edges; i++) {
cin>>x>>y>>weight;
x--; y--;
graph[x].push_back({y, weight});
graph[y].push_back({x, weight});
}
int src=1;
src--;
ll d[n];
for(i=0; i<n; i++) {
if(src==i) {
dist.insert({0, i});
d[i]=0;
}
else {
dist.insert({LLONG_MAX, i});
d[i]=LLONG_MAX;
}
}
bool decided[n]={0};
int path[n]={-1};
for(int i=1; i<n; i++) path[i]=-2;
dij(graph, decided, d, path);
if(path[n-1]==-2) cout<<-1;
else {
vector<int> s;
int final=n-1;
while (final!=-1) {
s.push_back(final);
final=path[final];
}
reverse(s.begin(), s.end());
for(auto pi:s) cout<<pi+1<<" ";
}
cout<<endl;
}
Below is the code that produces a correct output for this tc.
#include<bits/stdc++.h>
using namespace std;
# define ll long long int
ll update, previous;
set<pair<ll, int>> dist;
auto it=dist.begin();
int ind=0, n, i, j;
pair<ll, int>p;
void dij(vector<pair<int, ll>> tree[], bool decided[], int d[], int path[]) {
ind=0;
while(!dist.empty()) {
it=dist.begin();
if(it==dist.end()) return;
ind=it->second;
dist.erase(it);
decided[ind]=1;
for(j=0; j<tree[ind].size(); j++) {
update=d[ind]+tree[ind][j].second;
previous=d[tree[ind][j].first];
if(update<previous) {
p=make_pair(previous, tree[ind][j].first);
dist.erase(dist.find(p));
p=make_pair(update, tree[ind][j].first);
dist.insert(p);
path[tree[ind][j].first]=ind;
}
d[tree[ind][j].first]=min(update, previous);
}
}
}
int main()
{
ll edges;
ll x, y, weight;
cin>>n>>edges;
vector<pair<int, ll>> graph[n];
for(i=0; i<edges; i++) {
cin>>x>>y>>weight;
x--; y--;
graph[x].push_back({y, weight});
graph[y].push_back({x, weight});
}
int src=1;
src--;
int d[n];
for(i=0; i<n; i++) {
if(src==i) {
dist.insert({0, i});
d[i]=0;
}
else {
dist.insert({INT_MAX, i});
d[i]=INT_MAX;
}
}
bool decided[n]={0};
int path[n]={-1};
for(int i=1; i<n; i++) path[i]=-2;
dij(graph, decided, d, path);
if(path[n-1]==-2) cout<<-1;
else {
vector<int> s;
int final=n-1;
while (final!=-1) {
s.push_back(final);
final=path[final];
}
reverse(s.begin(), s.end());
for(auto pi:s) cout<<pi+1<<" ";
}
cout<<endl;
}
The only difference in the 2 codes are the following lines:
void dij(vector<pair<int, ll>> tree[], bool decided[], ll d[], int path[])
void dij(vector<pair<int, ll>> tree[], bool decided[], int d[], int path[])
ll d[n];
int d[n];
dist.insert({LLONG_MAX, i})
dist.insert({INT_MAX, i})
d[i]=LLONG_MAX
d[i]=INT_MAX
Could someone please point out how is this creating the following error which I read is related to "allocating memory where I should not" or "attempting to execute delete with a pointer value that was not obtained from new". What is causing this problem and how should I resolve it?
free(): invalid pointer
Aborted (core dumped)
The problem was indeed related to long long and that is why the code with int was running fine, because the fact that update would create an overflow, as the variable is a sum of two long long type variables which would have had max value LLONG_MAX assigned in main() was overlooked.
As long long can not accommodate 2*LLONG_MAX it was neither holding nor finding that value in the set of pairs used as the min heap. Thus iterator pointed to end of the set and erasing set.end() would generate undefined behavior in the long long datatype oriented code whereas it wouldn't in the int oriented one.
Replacing LLONG_MAX with 1e18 instead, in the code solved the problem and the code runs for all test files smoothly.
Additionally to clarify all the reasons that were pointed out through the comments, I thought I should clarify that not checking if dist.find(p) exists and doesn't point to end of set before performing dist.erase(dist.find(p)) would not create any problems. This is because it is Dijkstra's Algorithm and as many times as update is found to be less than previous, the node that this updated distance is being calculated for, from the source will always be present in the set paired with the distance previous. This is because all the nodes are initially entered with a value of 10e8 and are being updated as the values are found in successive iterations of the while loop.
Below is the working code, the only difference is that instead of LLONG_MAX I have used 1e18 and it runs fine on all test files including the one I had mentioned in the question as being problematic.
#include<bits/stdc++.h>
using namespace std;
# define ll long long int
ll update, previous;
set<pair<ll, int>> dist;
auto it=dist.begin();
int ind=0, n, i, j;
pair<ll, int>p;
void dij(vector<pair<int, ll>> tree[], bool decided[], ll d[], int path[]) {
ind=0;
while(!dist.empty()) {
it=dist.begin();
if(it==dist.end()) return;
ind=it->second;
dist.erase(it);
decided[ind]=1;
for(j=0; j<tree[ind].size(); j++) {
update=d[ind]+tree[ind][j].second;
previous=d[tree[ind][j].first];
if(update<previous) {
p=make_pair(previous, tree[ind][j].first);
//cout<<p.first<<" intermediate "<<p.second<<endl;
dist.erase(dist.find(p));
p=make_pair(update, tree[ind][j].first);
dist.insert(p);
path[tree[ind][j].first]=ind;
}
d[tree[ind][j].first]=min(update, previous);
}
}
}
int main()
{
ll edges;
ll x, y, weight;
cin>>n>>edges;
vector<pair<int, ll>> graph[n];
for(i=0; i<edges; i++) {
cin>>x>>y>>weight;
x--; y--;
graph[x].push_back({y, weight});
graph[y].push_back({x, weight});
}
int src=1;
src--;
ll d[n];
for(i=0; i<n; i++) {
if(src==i) {
dist.insert({0, i});
d[i]=0;
}
else {
dist.insert({1e18, i});
d[i]=1e18;
}
}
bool decided[n]={0};
int path[n]={-1};
for(int i=1; i<n; i++) path[i]=-2;
dij(graph, decided, d, path);
if(path[n-1]==-2) cout<<-1;
else {
vector<int> s;
int final=n-1;
while (final!=-1) {
s.push_back(final);
final=path[final];
}
reverse(s.begin(), s.end());
for(auto pi:s) cout<<pi+1<<" ";
}
cout<<endl;
}
Here is the output for the test file in the question:
Input
100 50
1 23 133
1 87 16
2 9 78
3 12 117
3 39 19
5 25 219
5 47 130
5 97 157
6 50 114
9 11 25
9 39 227
10 45 187
10 77 120
12 19 85
13 43 247
14 16 4
15 33 223
16 33 1
19 69 204
20 35 119
20 43 213
20 86 19
22 40 233
23 33 61
23 79 152
26 89 213
27 57 129
28 42 220
31 68 84
31 69 183
32 39 145
32 100 117
33 49 198
34 48 78
37 66 200
37 91 77
39 44 235
41 70 109
42 92 33
44 74 196
48 73 26
51 57 216
53 70 158
63 98 220
66 72 148
80 93 150
81 99 54
83 84 129
83 89 177
95 100 16
Participant's output
-1
Jury's answer
-1
The link to submission - Dijkstra

Problem in Knights Tour using backtracking

I am getting a infinite loop when I try and run my solution for the Knights Tour problem using Backtracking
My Solution Code:
Link: https://ideone.com/Ud92vF
code:
#include <bits/stdc++.h>
using namespace std;
bool valid(int arr[8][8],int r,int c)
{
if(r>=0 and r<8 and c>=0 and c<8 and arr[r][c]== -1)
return true;
return false;
}
void fun(int arr[8][8],int r,int c,int x)
{
if(x==64){
cout<<"***********************ARRAY FOUND***********************\n";
for(int i=0;i<8;i++){
for(int j=0;j<8;j++)
cout<<arr[i][j]<<" ";
cout<<"\n";
}
return;
}
if(!valid(arr,r,c))
return;
arr[r][c] = x;
fun(arr,r-2,c+1,x+1); fun(arr,r-2,c-1,x+1);
fun(arr,r-2,c+2,x+1); fun(arr,r-2,c-2,x+1);
fun(arr,r+2,c+1,x+1); fun(arr,r+2,c-1,x+1);
fun(arr,r+1,c+2,x+1); fun(arr,r+1,c-2,x+1);
arr[r][c] = -1;
}
int main()
{
int arr[8][8] ;
for(int i=0;i<8;i++){
for(int j=0;j<8;j++)
arr[i][j] = -1;
}
int r=0,c=0,x=0; fun(arr,r,c,x);
}
Make sure your move array is correct:
fun(arr,r-2,c-1,x+1); fun(arr,r-2,c+1,x+1);
fun(arr,r-1,c-2,x+1); fun(arr,r-1,c+2,x+1);
fun(arr,r+1,c-2,x+1); fun(arr,r+1,c+2,x+1);
fun(arr,r+2,c-1,x+1); fun(arr,r+2,c+1,x+1);
With this I get a right answer:
***********************ARRAY FOUND***********************
0 11 8 5 2 13 16 19
9 6 1 12 17 20 3 14
30 27 10 7 4 15 18 21
63 24 31 28 35 22 47 44
32 29 26 23 48 45 36 57
25 62 51 34 39 56 43 46
52 33 60 49 54 41 58 37
61 50 53 40 59 38 55 42
Note that as you use the 65th move to validate you answer, you'll get 8 of the same correct answers in a row. And then another 8. Etc. You can fix this by printing after your 64th move:
void fun(int arr[8][8],int r,int c,int x)
{
if(!valid(arr,r,c))
return;
arr[r][c] = x;
if(x==63){
cout<<"***********************ARRAY FOUND***********************\n";
for(int i=0;i<8;i++){
for(int j=0;j<8;j++)
cout<<arr[i][j]<<" ";
cout<<"\n";
}
}
else
{
fun(arr,r-2,c-1,x+1); fun(arr,r-2,c+1,x+1);
fun(arr,r-1,c-2,x+1); fun(arr,r-1,c+2,x+1);
fun(arr,r+1,c-2,x+1); fun(arr,r+1,c+2,x+1);
fun(arr,r+2,c-1,x+1); fun(arr,r+2,c+1,x+1);
}
arr[r][c] = -1;
}
And one last issue is that you only ever start at {0,0} so you'll only find knights tours which start on that square. You really want to start from every square to find all possible knights tours. Or if you're feeling clever you only need to check a subset of the starting squares and use symmetry to generate the others.

Binary Search TestCase Not Returning a Value

So I am trying to solve the following question
Input Format is
N
x x x x x ...
q
y y y y y ...
N=size of array
x,x,x ... are elements of array
q=no of queries
y,y,y .. are queries to be searched in the array using binary search
Here is My code
#include <iostream>
#include<algorithm>
using namespace std;
int main()
{
int N,q;
cin>>N;
int a[N];
for(int i=1;i<=N;i++)
{
cin>>a[i];
}
cin>>q;
int b[q];
for(int i=0;i<q;i++)
{
cin>>b[i];
}
int len=sizeof(a)/sizeof(a[1]);
sort(a,a+len);
int beg=1,end=N;
for(int j=0;j<q;j++)
{
beg=1;end=N;
while(beg<=end)
{
int mid=(beg+end)/2;
if(b[j]==a[mid])
{
cout<<mid<<endl;
break;
}
else if(b[j]<a[mid])
{
end=mid-1;
}
else
beg=mid+1;
}
}
return 0;
}
My code is giving the following output which is wrong
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
for the input
100
100 99 98 97 96 95 94 93 92 91 90 89 88 87 86 85 84 83 82 81 80 79 78 77 76 75 74 73 72 71 70 69 68 67 66 65 64 63 62 61 60 59 58 57 56 55 54 53 52 51 50 49 48 47 46 45 44 43 42 41 40 39 38 37 36 35 34 33 32 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1
20
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
correct output is
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
Arrays are 0-based.
Arrays are not variable-length in C++.
You have a wrong update here:
else if(b[j]<a[mid])
{
end=mid-1;
}
The end is non-inclusive.
You will also want to keep going until (beg<end) not beg<=mid - otherwise mid will simply equal both.
Here's C++ version that fixes all of the above and uses iterators instead of indexes. Iterators remove the ambiguity (base-0 vs base-1) and make it very explicit that a range is [begin, end), by contract.
Live ON Coliru
#include <algorithm>
#include <iostream>
#include <iterator>
#include <vector>
using namespace std;
int main() {
int N;
cin >> N;
std::vector<int> a(N);
std::copy_n(std::istream_iterator<int>(std::cin), N, a.begin());
int q;
cin >> q;
std::vector<int> b(q);
std::copy_n(std::istream_iterator<int>(std::cin), q, b.begin());
sort(a.begin(), a.end());
for (auto query : b) {
auto beg = a.begin();
auto end = a.end();
while (beg < end) {
auto mid = beg + (end-beg) / 2;
if (query == *mid) {
cout << *mid << endl;
break;
} else if (query < *mid) {
end = mid;
} else beg = mid + 1;
}
}
}
Prints
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
Problem 1
Your program has undefined behavior due to accessing a using an out of bounds index in the following loop.
for(int i=1;i<=N;i++)
{
cin>>a[i];
}
That loop needs to be changed to use a 0 based index.
for(int i = 0; i < N; i++)
{
cin >> a[i];
}
Problem 2
For similar reasons, the initial value of beg needs to be 0, not 1.
Problem 3
You are comparing with values of a[mid] but you are outputting mid. The output also needs to be a[mid].
Problem 4
else if(b[j]<a[mid])
{
end=mid-1;
}
needs to be
else if(b[j]<a[mid])
{
end=mid;
}
With the above changes, the program works as expected in my environment. Here's the updated program:
#include <iostream>
#include<algorithm>
using namespace std;
int main()
{
int N,q;
cin>>N;
int a[N];
for(int i=0;i<N;i++)
{
cin>>a[i];
}
cin>>q;
int b[q];
for(int i=0;i<q;i++)
{
cin>>b[i];
}
int len=sizeof(a)/sizeof(a[1]);
sort(a,a+len);
int beg=0,end=N;
for(int j=0;j<q;j++)
{
beg=0;end=N;
while(beg<end)
{
int mid=(beg+end)/2;
if(b[j]==a[mid])
{
cout << a[mid] << endl;
break;
}
else if(b[j]<a[mid])
{
end=mid;
}
else
beg=mid+1;
}
}
return 0;
}
See it working at https://ideone.com/wgF2IS.
Array start with 0 index.so make sure for N elements your loop must start with zero index.
for(int i = 0; i < N; i++)
{
cin >> a[i];
}
Similarly, for same reason assign beg '0' value and end 'N-1' value.

Using an STL map, wrong number of template arguments?

I'm having a little trouble utilizing a map, and I have never really used them before so I am really struggling here
My Code is as follows:
MSTapp.h
5 #ifndef MSTAPP_H
6 #define MSTAPP_H
7
8 #include"Graph.h"
9
10 #include<iostream>
11 #include<map>
12 #include<vector>
13 #include<string>
14
15 using namespace std;
16
17 class MSTapp
18 {
19 public:
20 void read_graph();
21 void print_v();
22 // void print_e();
23
24 private:
25 Graph my_graph;
26 };
27
28 #endif
29
MSTapp.cpp
5 #include"MSTapp.h"
6 #include"Graph.h"
7
8 #include<iostream>
9 #include<map>
10 #include<vector>
11 #include<stdlib.h>
12 #include<sstream>
13 #include<string>
14
15 using namespace std;
16
17 void MSTapp::read_graph()
18 {
19 string s;
20 int count = 0;
21
22 while(getline(cin, s))
23 {
24 if(count == 0)
25 {
26 istringstream my_words(s);
27 string word;
28 while(my_words >> word)
29 {
30 my_graph.add_vertex(word);
31 }
32 }
33 else
34 {
35 string first;
36 string last;
37 int key;
38
39 first = s.substr(0, s.find(" "));
40 s.erase(0,s.find(" ")+1);
41 last = s.substr(0, s.find(" "));
42 s.erase(0,s.find(" ")+1);
43 key = atoi(s.c_str());
44
45 my_graph.add_edge(first, last, key);
46 }
47 }
48 }
49
50 void MSTapp::print_v()
51 {
52 my_graph.print_vertices();
53 }
Graph.h
5 #include"MSTapp.h"
6 #include<map>
7 #include<iostream>
8 #include<string>
9 #include<vector>
10 #include<list>
11
12 using namespace std;
13
14 #ifndef GRAPH_H
15 #define GRAPH_H
16
17 class Graph
18 {
19 public:
20 // Graph();
21 // ~Graph();
22 void add_vertex(string name);
23 void print_vertices();
24 void add_edge(string from, string to, int weight);
25 // void print_edges();
26 // void min_span_tree(string start);
27
28 private:
29 // MinPriority min_queue;
30
31 class Vertex
32 {
33 public:
34 string name;
35 string pi;
36 int key;
37 };
38 class Neighbor
39 {
40 public:
41 string name;
42 int weight;
43 };
44
45 vector<Vertex> vertices;
46 map <string name, list<Neighbor> > adj_list;
4
48 };
49
50 #endif
Graph.cpp
5 #include"MSTapp.h"
6 #include"Graph.h"
7
8 #include<iostream>
9 #include<map>
10 #include<algorithm> // sort
11 #include<string>
12 #include<vector>
13 #include<list>
14
15 using namespace std;
35 void Graph::add_vertex(string name)
36 {
37 Vertex v1;
38 v1.name = name;
39 v1.pi = "NIL";
40 v1.key = 100;
41 bool check;
42
43 for(int i = 0; i < vertices.size(); i++)
44 {
45 if(vertices[i].name == v1.name)
46 {
47 check = true;
48 }
49 }
50
51 if(check == false)
52 {
53 vertices.push_back(v1);
54 }
55 }
56
57 void Graph::print_vertices()
58 {
59 for(int i = 0; i < vertices.size(); i++)
60 {
61 cout << vertices[i].name << " " << vertices[i].pi << " " << vertices[i].key << endl;
62 }
63 }
64
65 void Graph::add_edge(string from, string to, int weight)
66 {
67 string v_from = from;
68 string v_to = to;
69 int v_weight = weight;
70
71 Neighbor n1;
72 n1.name = v_to;
73 n1.weight = v_weight;
74
75 adj_list[v_from];
76 adj_list[v_from].push_back(n1);
77 adj_list[v_from].sort();
78 }
All the functions for the Vertices are working so just ignore those. The main problem I am having is the add_edge function in the Graph. For example my input would be something as follows:
A B C D E //Vertices, this is working
A B 3
so, with the A B 3, I would like to add the B and 3 the the neighbor, and do something like
adj_list[A].push_back(Neighbor)
The errors I am getting are regarding the map, it is saying I have the incorrect number of template arguments for the map in Graph.h.
Am I declaring my map incorrectly on line 46 of Graph.h?
If anyone could provide any insight as to what I am doing wrong or how I can get this working it would be GREATLY appreciated. If any additional clarifcation is needed, just ask. Thanks!
you should declare the map as:
map <string, list<Neighbor> > adj_list;