Display and add methods for adjacency list graph
This is my third time for creating a graph using adjacency list in c++. It's important to use OOP. I feel like the answer to this problem is really simple, but I can't manage to fix and improve my code.
There is it:
#include <iostream>
#include <algorithm>
#include <fstream>
#include <vector>
using namespace std;
struct Edge
{
int begin;
int end;
};
class Graph
{
private:
int numOfNodes;
vector<vector<int>> baseVec;
public:
Graph(int numOfNodes)
{
//baseVec->resize(numOfNodes, vector<int>(numOfNodes));
for (int i = 0; i < numOfNodes; i++)
{
vector<Edge> subVec;
baseVec.emplace_back(subVec);
}
}
void newEdge(Edge edge)
{
if (edge.begin >= numOfNodes && edge.end >= numOfNodes)
{
cout << "Invalid edge!n";
}
baseVec[edge.begin].emplace_back(edge.end);
baseVec[edge.end].emplace_back(edge.begin);
}
void display()
{
cout << baseVec.size();
for (int i = 0; i < baseVec.size(); i++)
{
cout << "n Adjacency list of vertex " << i << "n head ";
for (int j = 0; j < baseVec[i].size(); j++)
{
cout << baseVec[i][j];
cout << endl;
}
}
}
};
int main()
{
int vertex, numberOfEdges, begin, end;
cout << "Enter number of nodes: ";
cin >> vertex;
numberOfEdges = vertex * (vertex - 1);
Edge edge;
Graph g1(vertex);
for (int i = 0; i < numberOfEdges; i++)
{
cout << "Enter edge ex.1 2 (-1 -1 to exit): n";
cin >> edge.begin >> edge.end;
if ((begin == -1) && (end == -1))
{
break;
}
g1.newEdge(edge);
}
g1.display();
return 0;
}
So now in Visual Studio I have an error:
'std::vector>::vector(const std::vector<_Ty,std::allocator<_Ty>> &)': cannot convert argument 1 from 'std::vector>' to 'const _Alloc &'
Also that in display() method there is signed/unsigned mismatch. I don't know If there is something wrong with my methods but I'm stuck here.
c++ class graph-theory adjacency-list
|
show 4 more comments
This is my third time for creating a graph using adjacency list in c++. It's important to use OOP. I feel like the answer to this problem is really simple, but I can't manage to fix and improve my code.
There is it:
#include <iostream>
#include <algorithm>
#include <fstream>
#include <vector>
using namespace std;
struct Edge
{
int begin;
int end;
};
class Graph
{
private:
int numOfNodes;
vector<vector<int>> baseVec;
public:
Graph(int numOfNodes)
{
//baseVec->resize(numOfNodes, vector<int>(numOfNodes));
for (int i = 0; i < numOfNodes; i++)
{
vector<Edge> subVec;
baseVec.emplace_back(subVec);
}
}
void newEdge(Edge edge)
{
if (edge.begin >= numOfNodes && edge.end >= numOfNodes)
{
cout << "Invalid edge!n";
}
baseVec[edge.begin].emplace_back(edge.end);
baseVec[edge.end].emplace_back(edge.begin);
}
void display()
{
cout << baseVec.size();
for (int i = 0; i < baseVec.size(); i++)
{
cout << "n Adjacency list of vertex " << i << "n head ";
for (int j = 0; j < baseVec[i].size(); j++)
{
cout << baseVec[i][j];
cout << endl;
}
}
}
};
int main()
{
int vertex, numberOfEdges, begin, end;
cout << "Enter number of nodes: ";
cin >> vertex;
numberOfEdges = vertex * (vertex - 1);
Edge edge;
Graph g1(vertex);
for (int i = 0; i < numberOfEdges; i++)
{
cout << "Enter edge ex.1 2 (-1 -1 to exit): n";
cin >> edge.begin >> edge.end;
if ((begin == -1) && (end == -1))
{
break;
}
g1.newEdge(edge);
}
g1.display();
return 0;
}
So now in Visual Studio I have an error:
'std::vector>::vector(const std::vector<_Ty,std::allocator<_Ty>> &)': cannot convert argument 1 from 'std::vector>' to 'const _Alloc &'
Also that in display() method there is signed/unsigned mismatch. I don't know If there is something wrong with my methods but I'm stuck here.
c++ class graph-theory adjacency-list
semi-offtopic: here:{ vector<Edge> subVec; baseVec.emplace_back(subVec); }
why are you usingemplace_back
?emplace_back
allows you to create a element in place instead of first creating it and then copying into the container. First creating the element and then callingemplace_back
has it backwards. If you want to add n default constructed elements, you can simply callbaseVec.resize(baseVec.size()+n)
instead of writing the loop.
– user463035818
Nov 15 '18 at 20:45
Yes, definitely your way is much better. I'm going to change that and several more things. But now I'm wondering how to display the edges...
– Top4o
Nov 15 '18 at 20:51
stackoverflow.com/questions/2981836/how-can-i-use-cout-myclass
– user463035818
Nov 15 '18 at 20:52
So I think I overloaded the operator like that : std::ostream &operator<<(std::ostream &os, Edge const &m) { return os << m.begin << m.end; } But still I get the error: binary '<<': no operator found which takes a right-hand operand of type '_Ty' (or there is no acceptable conversion) . Probably I'm asking stupid questions, sorry about that.
– Top4o
Nov 15 '18 at 21:10
your have the out-of-bounds check wrong:if (edge.begin >= numOfNodes && edge.end >= numOfNodes)
the last valid index isnumOfNodes-1
notnumOfNodes
– user463035818
Nov 15 '18 at 21:14
|
show 4 more comments
This is my third time for creating a graph using adjacency list in c++. It's important to use OOP. I feel like the answer to this problem is really simple, but I can't manage to fix and improve my code.
There is it:
#include <iostream>
#include <algorithm>
#include <fstream>
#include <vector>
using namespace std;
struct Edge
{
int begin;
int end;
};
class Graph
{
private:
int numOfNodes;
vector<vector<int>> baseVec;
public:
Graph(int numOfNodes)
{
//baseVec->resize(numOfNodes, vector<int>(numOfNodes));
for (int i = 0; i < numOfNodes; i++)
{
vector<Edge> subVec;
baseVec.emplace_back(subVec);
}
}
void newEdge(Edge edge)
{
if (edge.begin >= numOfNodes && edge.end >= numOfNodes)
{
cout << "Invalid edge!n";
}
baseVec[edge.begin].emplace_back(edge.end);
baseVec[edge.end].emplace_back(edge.begin);
}
void display()
{
cout << baseVec.size();
for (int i = 0; i < baseVec.size(); i++)
{
cout << "n Adjacency list of vertex " << i << "n head ";
for (int j = 0; j < baseVec[i].size(); j++)
{
cout << baseVec[i][j];
cout << endl;
}
}
}
};
int main()
{
int vertex, numberOfEdges, begin, end;
cout << "Enter number of nodes: ";
cin >> vertex;
numberOfEdges = vertex * (vertex - 1);
Edge edge;
Graph g1(vertex);
for (int i = 0; i < numberOfEdges; i++)
{
cout << "Enter edge ex.1 2 (-1 -1 to exit): n";
cin >> edge.begin >> edge.end;
if ((begin == -1) && (end == -1))
{
break;
}
g1.newEdge(edge);
}
g1.display();
return 0;
}
So now in Visual Studio I have an error:
'std::vector>::vector(const std::vector<_Ty,std::allocator<_Ty>> &)': cannot convert argument 1 from 'std::vector>' to 'const _Alloc &'
Also that in display() method there is signed/unsigned mismatch. I don't know If there is something wrong with my methods but I'm stuck here.
c++ class graph-theory adjacency-list
This is my third time for creating a graph using adjacency list in c++. It's important to use OOP. I feel like the answer to this problem is really simple, but I can't manage to fix and improve my code.
There is it:
#include <iostream>
#include <algorithm>
#include <fstream>
#include <vector>
using namespace std;
struct Edge
{
int begin;
int end;
};
class Graph
{
private:
int numOfNodes;
vector<vector<int>> baseVec;
public:
Graph(int numOfNodes)
{
//baseVec->resize(numOfNodes, vector<int>(numOfNodes));
for (int i = 0; i < numOfNodes; i++)
{
vector<Edge> subVec;
baseVec.emplace_back(subVec);
}
}
void newEdge(Edge edge)
{
if (edge.begin >= numOfNodes && edge.end >= numOfNodes)
{
cout << "Invalid edge!n";
}
baseVec[edge.begin].emplace_back(edge.end);
baseVec[edge.end].emplace_back(edge.begin);
}
void display()
{
cout << baseVec.size();
for (int i = 0; i < baseVec.size(); i++)
{
cout << "n Adjacency list of vertex " << i << "n head ";
for (int j = 0; j < baseVec[i].size(); j++)
{
cout << baseVec[i][j];
cout << endl;
}
}
}
};
int main()
{
int vertex, numberOfEdges, begin, end;
cout << "Enter number of nodes: ";
cin >> vertex;
numberOfEdges = vertex * (vertex - 1);
Edge edge;
Graph g1(vertex);
for (int i = 0; i < numberOfEdges; i++)
{
cout << "Enter edge ex.1 2 (-1 -1 to exit): n";
cin >> edge.begin >> edge.end;
if ((begin == -1) && (end == -1))
{
break;
}
g1.newEdge(edge);
}
g1.display();
return 0;
}
So now in Visual Studio I have an error:
'std::vector>::vector(const std::vector<_Ty,std::allocator<_Ty>> &)': cannot convert argument 1 from 'std::vector>' to 'const _Alloc &'
Also that in display() method there is signed/unsigned mismatch. I don't know If there is something wrong with my methods but I'm stuck here.
c++ class graph-theory adjacency-list
c++ class graph-theory adjacency-list
edited Nov 15 '18 at 22:09
halfer
14.7k759116
14.7k759116
asked Nov 15 '18 at 19:49
Top4oTop4o
56
56
semi-offtopic: here:{ vector<Edge> subVec; baseVec.emplace_back(subVec); }
why are you usingemplace_back
?emplace_back
allows you to create a element in place instead of first creating it and then copying into the container. First creating the element and then callingemplace_back
has it backwards. If you want to add n default constructed elements, you can simply callbaseVec.resize(baseVec.size()+n)
instead of writing the loop.
– user463035818
Nov 15 '18 at 20:45
Yes, definitely your way is much better. I'm going to change that and several more things. But now I'm wondering how to display the edges...
– Top4o
Nov 15 '18 at 20:51
stackoverflow.com/questions/2981836/how-can-i-use-cout-myclass
– user463035818
Nov 15 '18 at 20:52
So I think I overloaded the operator like that : std::ostream &operator<<(std::ostream &os, Edge const &m) { return os << m.begin << m.end; } But still I get the error: binary '<<': no operator found which takes a right-hand operand of type '_Ty' (or there is no acceptable conversion) . Probably I'm asking stupid questions, sorry about that.
– Top4o
Nov 15 '18 at 21:10
your have the out-of-bounds check wrong:if (edge.begin >= numOfNodes && edge.end >= numOfNodes)
the last valid index isnumOfNodes-1
notnumOfNodes
– user463035818
Nov 15 '18 at 21:14
|
show 4 more comments
semi-offtopic: here:{ vector<Edge> subVec; baseVec.emplace_back(subVec); }
why are you usingemplace_back
?emplace_back
allows you to create a element in place instead of first creating it and then copying into the container. First creating the element and then callingemplace_back
has it backwards. If you want to add n default constructed elements, you can simply callbaseVec.resize(baseVec.size()+n)
instead of writing the loop.
– user463035818
Nov 15 '18 at 20:45
Yes, definitely your way is much better. I'm going to change that and several more things. But now I'm wondering how to display the edges...
– Top4o
Nov 15 '18 at 20:51
stackoverflow.com/questions/2981836/how-can-i-use-cout-myclass
– user463035818
Nov 15 '18 at 20:52
So I think I overloaded the operator like that : std::ostream &operator<<(std::ostream &os, Edge const &m) { return os << m.begin << m.end; } But still I get the error: binary '<<': no operator found which takes a right-hand operand of type '_Ty' (or there is no acceptable conversion) . Probably I'm asking stupid questions, sorry about that.
– Top4o
Nov 15 '18 at 21:10
your have the out-of-bounds check wrong:if (edge.begin >= numOfNodes && edge.end >= numOfNodes)
the last valid index isnumOfNodes-1
notnumOfNodes
– user463035818
Nov 15 '18 at 21:14
semi-offtopic: here:
{ vector<Edge> subVec; baseVec.emplace_back(subVec); }
why are you using emplace_back
? emplace_back
allows you to create a element in place instead of first creating it and then copying into the container. First creating the element and then calling emplace_back
has it backwards. If you want to add n default constructed elements, you can simply call baseVec.resize(baseVec.size()+n)
instead of writing the loop.– user463035818
Nov 15 '18 at 20:45
semi-offtopic: here:
{ vector<Edge> subVec; baseVec.emplace_back(subVec); }
why are you using emplace_back
? emplace_back
allows you to create a element in place instead of first creating it and then copying into the container. First creating the element and then calling emplace_back
has it backwards. If you want to add n default constructed elements, you can simply call baseVec.resize(baseVec.size()+n)
instead of writing the loop.– user463035818
Nov 15 '18 at 20:45
Yes, definitely your way is much better. I'm going to change that and several more things. But now I'm wondering how to display the edges...
– Top4o
Nov 15 '18 at 20:51
Yes, definitely your way is much better. I'm going to change that and several more things. But now I'm wondering how to display the edges...
– Top4o
Nov 15 '18 at 20:51
stackoverflow.com/questions/2981836/how-can-i-use-cout-myclass
– user463035818
Nov 15 '18 at 20:52
stackoverflow.com/questions/2981836/how-can-i-use-cout-myclass
– user463035818
Nov 15 '18 at 20:52
So I think I overloaded the operator like that : std::ostream &operator<<(std::ostream &os, Edge const &m) { return os << m.begin << m.end; } But still I get the error: binary '<<': no operator found which takes a right-hand operand of type '_Ty' (or there is no acceptable conversion) . Probably I'm asking stupid questions, sorry about that.
– Top4o
Nov 15 '18 at 21:10
So I think I overloaded the operator like that : std::ostream &operator<<(std::ostream &os, Edge const &m) { return os << m.begin << m.end; } But still I get the error: binary '<<': no operator found which takes a right-hand operand of type '_Ty' (or there is no acceptable conversion) . Probably I'm asking stupid questions, sorry about that.
– Top4o
Nov 15 '18 at 21:10
your have the out-of-bounds check wrong:
if (edge.begin >= numOfNodes && edge.end >= numOfNodes)
the last valid index is numOfNodes-1
not numOfNodes
– user463035818
Nov 15 '18 at 21:14
your have the out-of-bounds check wrong:
if (edge.begin >= numOfNodes && edge.end >= numOfNodes)
the last valid index is numOfNodes-1
not numOfNodes
– user463035818
Nov 15 '18 at 21:14
|
show 4 more comments
1 Answer
1
active
oldest
votes
vector<vector<int>> baseVec;
accepts vector<int>
s
vector<Edge> subVec;
baseVec.emplace_back(subVec);
Attempts to feed it vector<Edge>
s. This does not make sense.
vector<vector<Edge>> baseVec;
Makes better sense.
Note that this change will set up another blast of errors when
cout << baseVec[i][j];
tries to print an edge
and does not know how. Either make an operator<<
overload to handle edge
or change what you're outputting.
Thanks! That was helpful. You were right about cout errors, I'm going to try and fix them.
– Top4o
Nov 15 '18 at 20:22
Do you think that that code it's going to do the task of creating a Graph with edges? And if not what also should i change, I'm not sure about newEdge method.
– Top4o
Nov 15 '18 at 20:31
If you're not sure the code works, test it. Mind you, even if you're sure the code works you should test it just in case you get a nasty surprise. Ask a new question, if necessary, based on the results of your testing.
– user4581301
Nov 15 '18 at 20:35
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53326934%2fdisplay-and-add-methods-for-adjacency-list-graph%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
vector<vector<int>> baseVec;
accepts vector<int>
s
vector<Edge> subVec;
baseVec.emplace_back(subVec);
Attempts to feed it vector<Edge>
s. This does not make sense.
vector<vector<Edge>> baseVec;
Makes better sense.
Note that this change will set up another blast of errors when
cout << baseVec[i][j];
tries to print an edge
and does not know how. Either make an operator<<
overload to handle edge
or change what you're outputting.
Thanks! That was helpful. You were right about cout errors, I'm going to try and fix them.
– Top4o
Nov 15 '18 at 20:22
Do you think that that code it's going to do the task of creating a Graph with edges? And if not what also should i change, I'm not sure about newEdge method.
– Top4o
Nov 15 '18 at 20:31
If you're not sure the code works, test it. Mind you, even if you're sure the code works you should test it just in case you get a nasty surprise. Ask a new question, if necessary, based on the results of your testing.
– user4581301
Nov 15 '18 at 20:35
add a comment |
vector<vector<int>> baseVec;
accepts vector<int>
s
vector<Edge> subVec;
baseVec.emplace_back(subVec);
Attempts to feed it vector<Edge>
s. This does not make sense.
vector<vector<Edge>> baseVec;
Makes better sense.
Note that this change will set up another blast of errors when
cout << baseVec[i][j];
tries to print an edge
and does not know how. Either make an operator<<
overload to handle edge
or change what you're outputting.
Thanks! That was helpful. You were right about cout errors, I'm going to try and fix them.
– Top4o
Nov 15 '18 at 20:22
Do you think that that code it's going to do the task of creating a Graph with edges? And if not what also should i change, I'm not sure about newEdge method.
– Top4o
Nov 15 '18 at 20:31
If you're not sure the code works, test it. Mind you, even if you're sure the code works you should test it just in case you get a nasty surprise. Ask a new question, if necessary, based on the results of your testing.
– user4581301
Nov 15 '18 at 20:35
add a comment |
vector<vector<int>> baseVec;
accepts vector<int>
s
vector<Edge> subVec;
baseVec.emplace_back(subVec);
Attempts to feed it vector<Edge>
s. This does not make sense.
vector<vector<Edge>> baseVec;
Makes better sense.
Note that this change will set up another blast of errors when
cout << baseVec[i][j];
tries to print an edge
and does not know how. Either make an operator<<
overload to handle edge
or change what you're outputting.
vector<vector<int>> baseVec;
accepts vector<int>
s
vector<Edge> subVec;
baseVec.emplace_back(subVec);
Attempts to feed it vector<Edge>
s. This does not make sense.
vector<vector<Edge>> baseVec;
Makes better sense.
Note that this change will set up another blast of errors when
cout << baseVec[i][j];
tries to print an edge
and does not know how. Either make an operator<<
overload to handle edge
or change what you're outputting.
answered Nov 15 '18 at 20:09
user4581301user4581301
20.8k52033
20.8k52033
Thanks! That was helpful. You were right about cout errors, I'm going to try and fix them.
– Top4o
Nov 15 '18 at 20:22
Do you think that that code it's going to do the task of creating a Graph with edges? And if not what also should i change, I'm not sure about newEdge method.
– Top4o
Nov 15 '18 at 20:31
If you're not sure the code works, test it. Mind you, even if you're sure the code works you should test it just in case you get a nasty surprise. Ask a new question, if necessary, based on the results of your testing.
– user4581301
Nov 15 '18 at 20:35
add a comment |
Thanks! That was helpful. You were right about cout errors, I'm going to try and fix them.
– Top4o
Nov 15 '18 at 20:22
Do you think that that code it's going to do the task of creating a Graph with edges? And if not what also should i change, I'm not sure about newEdge method.
– Top4o
Nov 15 '18 at 20:31
If you're not sure the code works, test it. Mind you, even if you're sure the code works you should test it just in case you get a nasty surprise. Ask a new question, if necessary, based on the results of your testing.
– user4581301
Nov 15 '18 at 20:35
Thanks! That was helpful. You were right about cout errors, I'm going to try and fix them.
– Top4o
Nov 15 '18 at 20:22
Thanks! That was helpful. You were right about cout errors, I'm going to try and fix them.
– Top4o
Nov 15 '18 at 20:22
Do you think that that code it's going to do the task of creating a Graph with edges? And if not what also should i change, I'm not sure about newEdge method.
– Top4o
Nov 15 '18 at 20:31
Do you think that that code it's going to do the task of creating a Graph with edges? And if not what also should i change, I'm not sure about newEdge method.
– Top4o
Nov 15 '18 at 20:31
If you're not sure the code works, test it. Mind you, even if you're sure the code works you should test it just in case you get a nasty surprise. Ask a new question, if necessary, based on the results of your testing.
– user4581301
Nov 15 '18 at 20:35
If you're not sure the code works, test it. Mind you, even if you're sure the code works you should test it just in case you get a nasty surprise. Ask a new question, if necessary, based on the results of your testing.
– user4581301
Nov 15 '18 at 20:35
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53326934%2fdisplay-and-add-methods-for-adjacency-list-graph%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
semi-offtopic: here:
{ vector<Edge> subVec; baseVec.emplace_back(subVec); }
why are you usingemplace_back
?emplace_back
allows you to create a element in place instead of first creating it and then copying into the container. First creating the element and then callingemplace_back
has it backwards. If you want to add n default constructed elements, you can simply callbaseVec.resize(baseVec.size()+n)
instead of writing the loop.– user463035818
Nov 15 '18 at 20:45
Yes, definitely your way is much better. I'm going to change that and several more things. But now I'm wondering how to display the edges...
– Top4o
Nov 15 '18 at 20:51
stackoverflow.com/questions/2981836/how-can-i-use-cout-myclass
– user463035818
Nov 15 '18 at 20:52
So I think I overloaded the operator like that : std::ostream &operator<<(std::ostream &os, Edge const &m) { return os << m.begin << m.end; } But still I get the error: binary '<<': no operator found which takes a right-hand operand of type '_Ty' (or there is no acceptable conversion) . Probably I'm asking stupid questions, sorry about that.
– Top4o
Nov 15 '18 at 21:10
your have the out-of-bounds check wrong:
if (edge.begin >= numOfNodes && edge.end >= numOfNodes)
the last valid index isnumOfNodes-1
notnumOfNodes
– user463035818
Nov 15 '18 at 21:14