In vector what does it mean?

This is a discussion on In vector what does it mean? within the C++ Programming forums, part of the General Programming Boards category; G[u].push back(v); here G[5] is a vector u,v are int...

  1. #1
    Registered User
    Join Date
    Sep 2012
    Posts
    13

    In vector what does it mean?

    G[u].push back(v);

    here G[5] is a vector
    u,v are int

  2. #2
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,528
    Manasij Mukherjee | gcc-4.9.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  3. #3
    Registered User
    Join Date
    Sep 2012
    Posts
    13

    vector proble: pair

    Code:
    #include <algorithm>
    #include <iostream>
    
    #include <vector>
    
    
    using namespace std;
    
    
    const int MAXN = 100;
    
    int A[MAXN], B[MAXN];
    
    vector<int> g[MAXN];
    
    bool second_high_to_low(pair<int, int> a, pair<int, int> b){
      if (a.second != b.second) return a.second > b.second;
      return a.first < b.first;
    }
    
    pair<int, int> f(int u, int dad = -1){
      vector< pair<int, int> > p;
      for (int i = 0; i < g[u].size(); ++i){
        int v = g[u][i];
        if (v == dad) continue;
    
        p.push_back( f(v, u) );
      }
    
      sort(p.begin(), p.end(), second_high_to_low);
    
      int total = A[u];
      int inhand = A[u] - B[u];
      for (int i = 0; i < p.size(); ++i){
        if (inhand > p[i].first){
          inhand -= p[i].first - p[i].second;
        }else{
          total += p[i].first - inhand;
          inhand = p[i].second;
        }
      }
      return make_pair(total, inhand);
    }
    
    int main(){
      int n, Case = 1;
      while (cin >> n){
        if (n == 0) break;
        cout << "Case " << Case++ << ": ";
    
        for (int i = 0; i < n; ++i){
          int x, y, z;
          cin >> x >> y >> z;
          A[i] = x;
          B[i] = y + z;
          A[i] = max(A[i], B[i]);
    
          g[i].clear();
        }
    
        for (int i = 0; i < n - 1; ++i){
          int u, v;
          cin >> u >> v;
          u--, v--;
          g[u].push_back(v);
          g[v].push_back(u);
        }
    
    
        int ans = 3000;
        for (int i = 0; i < n; ++i){
    
          pair<int, int> p = f(i);
    
          ans = min(ans, p.first);
    
        }
    
        cout << ans << endl;
      }
      return 0;
    }
    I want to konw how:
    Code:
    pair<int, int> p = f(i);
    is transfered to
    Code:
    pair<int, int> f(int u, int dad = -1){
    it is really confusing......
    Code:
    how f(i) becomes f(int u,int dad=-1)?
    also g was declared as
    Code:
    vector<int> g[MAX];
    how come it becomes g[u][i];?
    Please help

  4. #4
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,762
    Quote Originally Posted by enakta13 View Post
    I want to konw how:
    Code:
    pair<int, int> p = f(i);
    is transfered to
    Code:
    pair<int, int> f(int u, int dad = -1){
    it is really confusing......
    Code:
    how f(i) becomes f(int u,int dad=-1)?
    It's a C++ feature called default arguments. Unless you provide a different argument for dad, it will be -1 in the function. What's the reason, you think? A default argument is a good way to add functionality or a fix to a function working in existing code without breaking existing code.

    also g was declared as
    Code:
    vector<int> g[MAX];
    how come it becomes g[u][i];?
    Please help
    It's inherent in the type of g. An array of vectors can be indexed that way.

    Please help yourself to the detailed reference on the web you were linked earlier.
    Last edited by whiteflags; 09-27-2012 at 11:58 PM.

  5. #5
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Quote Originally Posted by whiteflags View Post
    It's a C++ feature called default arguments. Unless you provide a different argument for dad, it will be -1 in the function. What's the reason, you think? A default argument is a good
    Maybe you didn't understand the question!He clearly says how f(i) becomes...

    He does not question about default values.He asks about vectors in his post

  6. #6
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,762
    You're an idiot. He asks about both vectors and default arguments in his post. All you have to do is read his code to notice this, but I will explain my answer.

    f() is a function.
    pair<int, int> is the return value, f is the function name, and there's the parameter list "(int u, int dad = -1)". dad = -1 in this context is a default argument. You can also see the function body.
    Quote Originally Posted by enakta13
    how f(i) becomes f(int u,int dad=-1)?
    I mean, if I read the question wrong, you're welcome to put some effort in and actually correct me.

    But I also answer his other question.
    Quote Originally Posted by enakta13
    how come it becomes g[u][i];?
    Perhaps I didn't answer it well, but what could I do? It would be nice if he knew more words.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. vector<vector<int> > access element.
    By nimitzhunter in forum C++ Programming
    Replies: 0
    Last Post: 01-23-2011, 05:14 AM
  2. Converting string vector to integer vector
    By CPlus in forum C++ Programming
    Replies: 4
    Last Post: 05-08-2010, 06:43 AM
  3. including templatized vector and vector
    By -EquinoX- in forum C++ Programming
    Replies: 1
    Last Post: 11-22-2009, 04:46 AM
  4. Simple Question memset(vector<int>, 0, sizeof(vector<int>))
    By HyperShadow in forum C++ Programming
    Replies: 6
    Last Post: 12-10-2007, 04:56 PM
  5. vector of pointers vs vector of values
    By Shadow12345 in forum C++ Programming
    Replies: 1
    Last Post: 12-07-2002, 02:27 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21