How do i call that function so that i can use the vector in main?
thanks!
Code:vector<string> function(); int main() { //what goes here? } vector<string> function() { vector<string> s(3); s[0] = "one"; s[1] = "two"; s[2] = "three"; return s; }
How do i call that function so that i can use the vector in main?
thanks!
Code:vector<string> function(); int main() { //what goes here? } vector<string> function() { vector<string> s(3); s[0] = "one"; s[1] = "two"; s[2] = "three"; return s; }
Code:int main() { //what goes here? vector< string > vec( function() ); }
The return Type of the function is a vector<string> so it has to return a vector of strings... all you do is create another vector<string> in main and asign it the function.
Just as Antigloss showed you
You have a variety of options depending on your knowledge base and needs. Antigloss' version uses the copy constructor of vectors to transfer information from
from function() to main(). However, you could use the assignment operator:
or you could use a single vector passed back and forth by reference:Code:vector<string> function(); int main() { vector<string> vec; vec = function(); } vector<string> function() { vector<string> s(3); s[0] = "one"; s[1] = "two"; s[2] = "three"; return s; }
Code:void function(vector<string> &); int main() { vector<string> s(3); function(s); } void function(vector<string> & rS) { rS[0] = "one"; rS[1] = "two"; rS[2] = "three"; }
Last edited by elad; 07-18-2005 at 09:24 AM.
You're only born perfect.
thanks, i could not figure out how to assign that vector in the main.