এখানে অপারেটর ওভারলোড পদ্ধতি ব্যবহার করা হয়েছে। এই কাজ compare ফাংশন ব্যাবহার করেও করা যায় ।
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <algorithm> | |
#include <vector> | |
using namespace std; | |
struct edge | |
{ | |
int u, v; | |
int weight; | |
// sort by weight and here we overload < operator. | |
bool operator < (const edge &p) const { | |
return weight < p.weight; | |
} | |
}; | |
vector <edge> e; | |
int main() | |
{ | |
int nodes, edges; | |
cin >> nodes >> edges; | |
for(int i = 0; i < edges; i++) | |
{ | |
edge get; | |
int u, v, weight; | |
cin >> u >> v >> weight; | |
get.u = u; | |
get.v = v; | |
get.weight = weight; | |
e.push_back(get); | |
} | |
sort(e.begin(), e.end()); | |
for(int i = 0; i < edges; i++) | |
{ | |
// to access the value of structure list | |
// we can use vector_name[counter].member_name; | |
cout <<"first_node: " << e[i].u << " second_node : "<< e[i].v; | |
cout << " cost : " << e[i].weight << endl; | |
} | |
return 0; | |
} |
Comments
Post a Comment