Thread: is push_back() a overloaded function ?

  1. #1
    Registered User
    Join Date
    Dec 2003
    Posts
    92

    is push_back() a overloaded function ?

    hi, is push_back() a overloaded function?

    bcoz, probabily push_back() accepts simple argument and also pointer both.

    is it a overloaded function ? if so, is there any other way acceptance rule ?
    blue_gene

  2. #2
    unleashed alphaoide's Avatar
    Join Date
    Sep 2003
    Posts
    696
    source: compsci textbooks, cboard.cprogramming.com, world wide web, common sense

  3. #3
    Registered User
    Join Date
    Dec 2003
    Posts
    92
    ok,....it is written void push_back( const TYPE &val );

    so, pass by reference (alias).

    i have written a small code to see the behaviour...

    Code:
    #include<iostream>
    #include<vector>
    using namespace std;
    int main()
    {
    vector<int> z;
    int* p = new int[3];
    p[0]=6;
    z.push_back(p); // illegal
    z.push_back(p[0]); 
    cout<<z[0]<<endl;
    
    }

    Now suppose the vector contains array of pointers (say vector<int* > ) then how do i insert pointers into the vector ?? push_back() function wont allow me to enter pointer so whats the way to enter pointer into the vector ?
    Last edited by blue_gene; 05-01-2004 at 11:08 AM.
    blue_gene

  4. #4
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Posts
    2,572
    try this:
    Code:
    #include<iostream>
    #include<vector>
    using namespace std;
    int main()
    {
    	vector<int> z;
    	int* p = new int[3];
    	p[0]=6;
    	z.push_back(*p);  
    	cout<<z[0]<<endl;
    
    	return 0;
    }

    some entropy with that sink? entropysink.com

    there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka

  5. #5
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Posts
    2,572
    it basically works the same way as if you would try to do this:

    Code:
    cout << p << endl;  //this wont give you an error on most compilers 
                                    //but will print out some gibberish
    
    cout << *p << endl; //if you do this, the program will print out the first element
                                     //of the array that p points to, hence, 6

    some entropy with that sink? entropysink.com

    there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka

  6. #6
    Registered User
    Join Date
    Dec 2003
    Posts
    92
    no that code was an example. my las question was not meant for that.

    forget about my code.



    my question > is it possible to create a vector<some_type *> ?? how do i insert pointers into the vector ?

    say i want to do something like....

    vector{ p1,p2,p3,p4} // where p1,p2,p3,p4...etc are pointers to something.

    is it possible ?

    i am learning . any help is appreciated.

    thanks
    blue_gene

  7. #7
    unleashed alphaoide's Avatar
    Join Date
    Sep 2003
    Posts
    696
    Code:
    #include<iostream>
    #include<vector>
    using namespace std;
    int main()
    {
        vector<int *> z;             // create a container for pointer to int
        int* p = new int[3];
        p[0]=6;
        z.push_back(p);             // push a pointer to int
        cout<< *z[0] <<endl;     // use * to print the value of int
    }
    source: compsci textbooks, cboard.cprogramming.com, world wide web, common sense

  8. #8
    Registered User
    Join Date
    May 2003
    Posts
    161
    is it possible to create a vector<some_type *>
    Yes, and the syntax is just as you have it. Say you wanted to create a vector of pointers to int:
    Code:
    int num = 10;
    vector<int*> vec;
    vec.push_back(&num);
    Just remember that the regular warnings about pointers still apply. If they point to local variables, then they become invalid when the function exits. If they point to dynamically allocated memory, then you have to remember to free them when you are done.

    -tf

  9. #9
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    I think it would be a problem trying to put an array of integers into a vector because it wouldn't know how large the array is. If you are using vectors you might as well do something like this:
    Code:
    #include<iostream>
    #include<vector>
    using namespace std;
    int main()
    {
    vector<vector<int> > z;
    vector<int> y;
    y.push_back(0);
    y.push_back(1);
    
    z.push_back(y); 
    
    cout<<z[0][0]<<endl;
    cout<<z[0][1]<<endl;
    
    }
    You might, however, be able to use a char array, provided it is null-terminated:
    Code:
    #include<iostream>
    #include<vector>
    using namespace std;
    int main()
    {
    vector<char* > z;
    char y[2];
    y[0]='a';
    y[1]='b';
    y[2]='\0';
    z.push_back(y);
    cout<<z[0]<<endl;
    cout<<z[1]<<endl;
    
    }
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  10. #10
    Registered User
    Join Date
    Dec 2003
    Posts
    92
    just a quick question by looking ur code.

    Code:
    int num = 10;
    vector<int*> vec;
    vec.push_back(&num); //  u r sending a pointer(address ) but push back demands alias  . pointer(here by using & ) and references are not same thing.
    blue_gene

  11. #11
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    I believe alphaoide's solution will work...I wasn't thinking that the vector doesn't need to know the size, it just has the address of the first element in the array. You just need to dereference it or use the subscript operator as if you are working with an ordinary array.
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  12. #12
    Registered User
    Join Date
    Dec 2003
    Posts
    92
    >>I believe alphaoide's solution will work.....

    its giving compile error. i already checked that in my system . i copied and pasted.

    edit: i have ideas on ur example. i was curious to know how can i create vector<some_type *>.
    Last edited by blue_gene; 05-01-2004 at 11:56 AM.
    blue_gene

  13. #13
    unleashed alphaoide's Avatar
    Join Date
    Sep 2003
    Posts
    696
    Quote Originally Posted by blue_gene
    >>I believe alphaoide's solution will work.....

    its giving compile error. i already checked that in my system . i copied and pasted.
    What compile error? It works on mine
    source: compsci textbooks, cboard.cprogramming.com, world wide web, common sense

  14. #14
    Registered User
    Join Date
    Dec 2003
    Posts
    92
    g++ compiler....
    Last edited by blue_gene; 05-01-2004 at 12:09 PM.
    blue_gene

  15. #15
    unleashed alphaoide's Avatar
    Join Date
    Sep 2003
    Posts
    696
    it works on both my vc++ 6.0 and g++
    source: compsci textbooks, cboard.cprogramming.com, world wide web, common sense

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 06-04-2008, 05:57 PM
  2. dllimport function not allowed
    By steve1_rm in forum C++ Programming
    Replies: 5
    Last Post: 03-11-2008, 03:33 AM
  3. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  4. Problem with overloaded function templates
    By New++ in forum C++ Programming
    Replies: 10
    Last Post: 09-05-2005, 04:00 PM
  5. Cannot resolve overloaded function
    By Mithoric in forum C++ Programming
    Replies: 10
    Last Post: 11-29-2003, 03:40 AM