Thread: Vector -> int

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    22

    Vector -> int

    Code:
    #include <cstdlib> 
    #include <iostream>
    #include <vector>
    
    using namespace std;
    
    void print(vector <int> *xxx);
    
    int main(){
    vector <int> xxx;
    for (int i = 0;i<10;i++)
      xxx.push_back(i);
    
    print(&xxx);
    }
    
    void print(vector <int> *xxx){
     int one = xxx[0];                           // <--- ERROR
     int two = xxx[1];                           // <--- ERROR
     cout << "one = " one << endl;
     cout << "two = " two << endl;
    }
    When trying to run this I am getting:
    cannot convert `std::vector<int, std::allocator<int> >' to `int' in initialization
    What am I doing wrong ?

  2. #2
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    xxx is a pointer to a vector. Therefore xxx[0] is a vector.

  3. #3
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Why don't you pass a reference instead of a pointer?

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    > cout << "one = " one << endl;
    > cout << "two = " two << endl;
    Also you are missing some << operators on these two lines.

  5. #5
    Registered User
    Join Date
    Mar 2008
    Posts
    22
    Swoopy:
    That is not the actual program, but a quick sample showing the problem, I typed it fast here, and I forgot to place "<<"

    anyway here is my actual program, now I am passing the vector by refrance.
    Code:
    #include <cstdlib> 
    #include <iostream>
    #include <vector>
    
    using namespace std;
    
    long int size = 10000;
    void print(vector <long int> &arr, int size);
    void h_ify(vector <long int> &arr,int i);
    void make(vector <long int> &arr);
    void sort_me(vector <long int> &arr);
    int left(int i) ;
    int right(int i) ;
    ///////////////////////////////
    
    int main() 
    {
    vector <long int> a ; 
    
    srand((unsigned)time(0));
    for (int i = 0; i <= size; i++){     
        int x = rand(); 
        a.push_back(x);
    }
       
       sort_me(a);
    
      system ("pause");
     return 0;
    }
    
    ///////////////////////////////
    
    void print(vector <long int> &a, int size) {
      for (int i = 0; i < size; i++) {
        cout << a[i] << " ";
      }
      cout << endl;
    }
    
    ///////////////////////////////
    
    int parent(int i) 
    {
    if(i==1)
    return 0;
    
    if(i&#37;2==0)
        return ( (i / 2)-1);
    else
        return ( (i / 2));
    }
    
    int left(int i) {
      return (2 * i) + 1;
    }
    
    int right(int i) {
      return (2 * i) + 2;
    }
    ///////////////////////////////
    
    
    void h_ify(vector <long int> &a,int i) {
      int great;
      int l = left(i);
      int r = right(i);
      
      if ( (a[l] > a[i]) && (l < size)) 
        great = l;
      else 
        great = i;
    
      
      if ( (a[r] > a[great]) && (r < size)) 
        great = r;
    
    
      if (great != i) {
        int temp = a[i];
        a[i] = a[great];
        a[great] = temp;
        h_ify(a, great);
      }
      
    
    }
    
    ///////////////////////////////
    
    void make(vector <long int> &a) {
    for (int i = (size - 1) / 2; i >= 0; i--) {
        h_ify(a, i);
       // print(a, size);
      }
    }
    
    ///////////////////////////////
    
    void sort_me(vector <long int> &a) {
        
      make(a);
     
      for (int i = size; i > 0; i--) {
        int temp = a[0];
        a[0] = a[size - 1];
        a[size - 1] = temp;
        size = size - 1;
        h_ify(a, 0);
      }
    }

    This works, only when size is smaller then about 3500 anything larger the program crashes, why and how can I fixt this problem ?
    Last edited by Tupcia; 05-20-2008 at 10:43 PM.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    The depth of recursion of h_ify may have something to do with it.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Code:
     int one = (*xxx)[0];                           // <--- FIXED
     int two = (*xxx)[1];                           // <--- FIXED
    Dont treat a pointer to a vector as if it were an array. You need to dereference that pointer to get to the vector first.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  8. #8
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    This line will cause a crash:
    Code:
      if ( (a[l] > a[i]) && (l < size))
    You need to reverse the order of the ANDed expressions. First test l < size and then index into the vector with l.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  9. #9
    The larch
    Join Date
    May 2006
    Posts
    3,573
    By the way, you don't need to pass the size argument with vector. The vector already knows its size which is available to you through the size() member.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  2. Replies: 2
    Last Post: 03-24-2006, 08:36 PM
  3. getting a headache
    By sreetvert83 in forum C++ Programming
    Replies: 41
    Last Post: 09-30-2005, 05:20 AM
  4. Quack! It doesn't work! >.<
    By *Michelle* in forum C++ Programming
    Replies: 8
    Last Post: 03-02-2003, 12:26 AM
  5. easy if you know how to use functions...
    By Unregistered in forum C Programming
    Replies: 7
    Last Post: 01-31-2002, 07:34 AM