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 ?
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
source: compsci textbooks, cboard.cprogramming.com, world wide web, common sense
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 pointerso whats the way to enter pointer into the vector ?
Last edited by blue_gene; 05-01-2004 at 11:08 AM.
blue_gene
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
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
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
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
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:
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<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; }
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
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
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
>>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
That won't work as intended.Originally Posted by JaWiB
The first cout will print 'ab', the second is undefined behaviour.
Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling
Yes, and the syntax is just as you have it. Say you wanted to create a vector of pointers to int:is it possible to create a vector<some_type *>
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.Code:int num = 10; vector<int*> vec; vec.push_back(&num);
-tf
it works on both my vc++ 6.0 and g++
source: compsci textbooks, cboard.cprogramming.com, world wide web, common sense
i am so sorry. its working. may be that was a bad copy.i checked again. this time its working.
did you notice push_back() is taking a pointer p . but when i usued pointer p in my original code push_back() gave me an error. thats why i wrote //illegal.
what push_back() actually demands ? does it demand pointer and argument both . then i have to call it a overloaded function !!
its really a peculiar behaviour of push_back().....
i may be wrong somewhere.....
blue_gene