Advertisement
maycod23

Vector_Implementation

Feb 26th, 2022
683
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.10 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int main()
  5. {
  6.     //declaration
  7.     // vector<int> v(4);
  8.     // for (int i = 0; i < 4; i++) cin >> v[i];
  9.     // for (int i = 0; i < 4; i++) cout << v[i] << " ";
  10.     // cout << endl;
  11.  
  12.     // vector<int> v;//empty vector
  13.     // int n; cin >> n;
  14.     //push_back()->insertion and size allocation at the same time
  15.     // for (int i = 0; i < n; i++)
  16.     // {
  17.     //  int x; cin >> x;
  18.     //  v.push_back(x);
  19.     // }
  20.     //v -> curr size==n
  21.     // int x; cin >> x;
  22.     // v.push_back(x);
  23.  
  24.  
  25.     //v.size()->current size of the vector
  26.     // v->      {1,2,3}  v.size()=3
  27.     // indexes->{0,1,2}
  28.     // for (int i = 0; i < v.size(); i++)
  29.     // {
  30.     //  cout << "Index-> " << i << "Curr_element-> " << v[i] << endl;
  31.     // }
  32.  
  33.     //v.pop_back()
  34.     // for (int i = 0; i < v.size(); i++)
  35.     // {
  36.     //  cout << v[i] << " ";
  37.     // }
  38.     // cout << endl;
  39.     // cout << v.size() << " vector_curr_size " << endl;
  40.  
  41.     // v.pop_back();
  42.  
  43.     // cout << v.size() << " vector_curr_size " << endl;
  44.     // for (int i = 0; i < v.size(); i++)
  45.     // {
  46.     //  cout << v[i] << " ";
  47.     // }
  48.  
  49.     //vector<char> v;
  50.     //vector<string> v;
  51.  
  52.     // vector<string> v;
  53.     // cout << v.size() << endl;
  54.     // int n; cin >> n;
  55.     // for (int i = 0; i < n; i++)
  56.     // {
  57.     //  // cin >> v[i];->this will give error
  58.     //  string temp; cin >> temp;
  59.     //  v.push_back(temp);
  60.     //  cout << v.size() << endl;
  61.     //  // v.push_back(v[i])
  62.     // }
  63.     // for (int i = 0; i < v.size(); i++)
  64.     // {
  65.     //  cout << v[i] << " ";
  66.     // }
  67.     // cout << endl;
  68.  
  69.     //vector<pair<int,int>>;
  70.     //declaration
  71.     // pair<string, string> p;
  72.     // cin >> p.first >> p.second;
  73.     // cout << p.first << " " << p.second << " " << endl;
  74.  
  75.     // vector<pair<string, int>> v;
  76.     // int n; cin >> n;
  77.     // for (int i = 0; i < n; i++)
  78.     // {
  79.     //  pair<string, int> temp_pair;
  80.     //  cin >> temp_pair.first >> temp_pair.second;
  81.     //  v.push_back(temp_pair);
  82.     // }
  83.     // cout << v.size() << endl;
  84.  
  85.     // for (int i = 0; i < v.size(); i++)
  86.     // {
  87.     //  cout << v[i].first << " " << v[i].second << " " << endl;
  88.     // }
  89.  
  90.     // vector<pair<string, pair<string, int>>> v(3);
  91.     // for (int i = 0; i < v.size(); i++)
  92.     // {
  93.     //  cin >> v[i].first >> v[i].second.first >> v[i].second.second;
  94.     // }
  95.  
  96.     // cout << v.size() << endl; //->curr size
  97.     // for (int i = 0; i < v.size(); i++)
  98.     // {
  99.     //  cout << "Stu_Name " << v[i].first << " " << "Skill_Name " << v[i].second.first << " " << "Skill_Lev " << v[i].second.second << " " << endl;
  100.     // }
  101.  
  102.     int n, m; cin >> n >> m;
  103.     //n number of nodes, m number of edges
  104.     vector<vector<pair<int, int>>> v(n + 1);
  105.     for (int i = 1; i <= m; i++)
  106.     {
  107.         int a, b, w; cin >> a >> b >> w;
  108.         //a is connected to b with weight w
  109.         //b is connected to a with weight w
  110. //or we can also say that
  111.         //b is the children of a with weight w
  112.         //a is the children of b with weight w
  113.  
  114.         v[a].push_back(make_pair(b, w));
  115.         v[b].push_back(make_pair(a, w));
  116.  
  117.     }
  118.  
  119.     for (int i = 1; i < v.size(); i++)
  120.     {
  121.         cout << "Parent-> " << i << " " << endl;
  122.         for (int j = 0; j < v[i].size(); j++)
  123.         {
  124.             cout << "Child " << v[i][j].first << " " << "Weight " << v[i][j].second << " " << endl;
  125.         }
  126.         cout << endl; cout << endl;
  127.     }
  128.     return 0;
  129.  
  130. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement