Friday, July 31, 2009

How do I overload the ">>" function to take vectors?

In C++ how can this be done? I've been trying this literally all day... So far I have this:


istream%26amp; operator%26gt;%26gt; (istream%26amp; stream, vector%26lt; int %26gt;%26amp; a )


{


stream%26gt;%26gt;a;





return stream;


}








and it exits after I put in one input from the main function?


What should I do?

How do I overload the "%26gt;%26gt;" function to take vectors?
Hello,





You are misusing the contents of the overload operator, you cannot simply do stream %26gt;%26gt; a; You have to actually use the stream operator to do some stuff to the input. Since your using a vector, you need to add items inside the vector.





For example, you would need to treat the operator %26gt;%26gt; to what the stream operator will act. To make that work say we are going to use a cin as a stream input. For cin, if we are doing stream %26gt;%26gt; input, input must be a string for it to work, you cannot directly input it to a Vector. Since the vector contains Integer Values, you must input an integer value...





---


// Input string variable


string input;


// I am assuming I am doing cin %26gt;%26gt; myVector


// So %26gt;%26gt; in reality is a String


stream %26gt;%26gt; input;


// We must convert string to int to place into vector


// Using the #include %26lt;sstream %26gt; to stream it into an int


stringstream tempStream(input);


int tempInt;


tempStream %26gt;%26gt; tempInt;


// Push that int to the Vector


a.push_back( tempInt );


return stream;


---





Using the above example as the overloaded operator method, it will add the number inputed by cin stream to that vector. I could do the following to make it work. It will print out 3 as the size of the vector.





----





vector%26lt;int%26gt; a;


cout %26lt;%26lt; "Enter a number";


cin %26gt;%26gt; a;


cout %26lt;%26lt; "Enter a number";


cin %26gt;%26gt; a;


cout %26lt;%26lt; "Enter a number";


cin %26gt;%26gt; a;


cout %26lt;%26lt; "Size of Vector: " %26lt;%26lt; a.size() %26lt;%26lt; endl;


----





I will show you a simple way to add items to the vector in a loop, using the cin %26gt;%26gt; myVector. So I am going to overload the Vector with the cin stream so that I can always add items into the vector without stopping until I say CTRL+Z. You can look at the final result C++ class nicely formatted here: http://mohamed.mansour.pastebin.com/f4c0...








The main program will look like the following:


--------------------------------------...


int main()


{


// The vector


vector%26lt;int%26gt; a;





// Lets input values to that vector


// Overolading


cin %26gt;%26gt; a;





// Print the size of the vector


cout %26lt;%26lt; "Size of Vector: " %26lt;%26lt; a.size() %26lt;%26lt; endl;


return 0;


}


--------------------------------------...








The program output will look like the following:


--------------------------------------...


Enter your Input: Ctrl-Z to quit


1


Enter another Input: Ctrl-Z to quit


2


Enter another Input: Ctrl-Z to quit


3


Enter another Input: Ctrl-Z to quit


^Z


Size of Vector: 3


Press any key to continue . . .


--------------------------------------...





As you notice, I overloaded the Vector for the stream which is cin. And the output was nice and cool. You cannot say stream %26gt;%26gt;, because you have to somehow deal with that stream. I am using it with getline. You could expand this overload to decide what Stream object is going in, by just checking what type it is. For this problem, it is a cin stream.


--------------------------------------...


//overloaded input operator


istream%26amp; operator%26gt;%26gt; (istream%26amp; stream, vector%26lt; int %26gt;%26amp; a )





{


string input;


cout %26lt;%26lt; "Enter your Input: Ctrl-Z to quit" %26lt;%26lt; endl;


while(getline(stream,input))


{


// Convert from string to int


stringstream tempStream(input);


int tempInt;


tempStream %26gt;%26gt; tempInt;





// Push it to the Vector


a.push_back( tempInt );


cout %26lt;%26lt; "Enter another Input: Ctrl-Z to quit" %26lt;%26lt; endl;


}


return stream;


}


--------------------------------------...





AS you notice, it worked great, I treated the input stream and overloaded its operator. I hope that will help you do whatever you need accomplished.





Good Luck
Reply:%26lt;Chuck Norris%26gt;%26lt;ROBIN WILLIAMS%26gt;%26lt;BLAH%26gt;

apricot

No comments:

Post a Comment