954,498 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

vector push_back function not working

Hi i'm making a function that will return make a series of strings and perform a function returning a value for each. I've stored the strings in one vector v, and the integer values in another v2. Then it is supposed to return the string that corresponds to the smallest value in v2. I'm trying to pushback the vector for each time the for loop is run, but I get an error saying there is no such function "push_back" to call. What am I doing wrong here? I'm compiling on quincy by the way.

Thanks for the help!

string mutated_chooser(string origmutated,string orisentance, int offspringnumber,int rate){
std::vector <string> v;
std::vector <int> v2;

	for (int t=0; t=offspringnumber-1;t++){
		v.push_back(1);
		v2.push_back(1);
		string newmutated= mutatedstring(origmutated,rate);
		v[t]=newmutated;
		int matchnumber= match(orisentance,newmutated);
		v2[t]=matchnumber;}
		
	int min_index=min_element( v2.begin(), v2.end() ) - v2.begin();
	string closest =v[min_index];
	return v[min_index];
		
		}
hamby
Newbie Poster
11 posts since Jan 2012
Reputation Points: 6
Solved Threads: 0
 

Only thing I can think of is the includes that you have..

You need ,

triumphost
Posting Whiz
390 posts since Oct 2009
Reputation Points: 57
Solved Threads: 36
 

It probably said that std::vector has no function push_back(int) because on line 6 you say v.push_back(1), but v is a string vector, not an int vector.

Labdabeta
Posting Pro in Training
489 posts since Feb 2011
Reputation Points: 27
Solved Threads: 18
 

In "push_back()" you are supposed to input the value which you are adding to the vector.

For example, v.push_back("hello")

Perhaps you were under the impression that the value you put in push_back is the number of new slots being created. I believe your code should look like this:

for (int t=0; t=offspringnumber-1;t++){
		string newmutated= mutatedstring(origmutated,rate);
		v.push_back(newmutated);
		int matchnumber= match(orisentance,newmutated);
		v2.push_back(matchnumber);
}


You don't actually have to use "t" inside the for loop since the push_back function automatically sticks the new value on the end.

MareoRaft
Junior Poster in Training
79 posts since Oct 2005
Reputation Points: 10
Solved Threads: 4
 

Spot on! Thank you

hamby
Newbie Poster
11 posts since Jan 2012
Reputation Points: 6
Solved Threads: 0
 

Thanks, I filled the parameters of the vectors with the appropriate strings and integers. As mareoraft said I assumed the push_back function only did push back the vector the parameter's amount and didn't realise it actually filled the new element with the parameter.

hamby
Newbie Poster
11 posts since Jan 2012
Reputation Points: 6
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: