This is indeed an assignment, but I am stuck, it may just be the wording that I am not getting.
Make a function with variable number of arguments that searches the rest of the arguments for a sub-string provided as the first argument. This function will return a vector containing all arguments that contain the sub-string
This is what I have so far:
#include <iostream>
#include <string>
#include <algorithm>
#include <stdarg.h>
#include <vector>
using namespace std;
vector<string> search(string str, ...);
int main (){
va_list arguments;
char contin = 'y';
string str;
va_start(arguments, str);
cout << "Enter a string that will be searched for in the rest of the strings: ";
str += va_arg(arguments, str);
while(contin == 'y'){
cout << "Enter a string that will be searched in: ";
str += va_arg(arguments, str);
cout << endl << "Enter \''y\'' to continue or \''n\'' to end to start seach:" << endl;
cin >> contin;
}
va_end(arguments);
search(arguments);
return 0;
}
vector<string> search(string str, ... ){
va_list containing;
va_start (containing, str);
if (std::find(str.begin(), str.end(), str.front()) != str.end()){
str += va_arg(containing, str);
}
return containing;
}
I get these errors: Line 37: "str += va_arg(containing, str);" -error C2059: syntax error : ')'
Line 40: "return containing;" - error C2664: 'std::vector<_Ty>::vector(const std::vector<_Ty> &)' : cannot convert parameter 1 from 'va_list' to 'const std::vector<_Ty> &'
Line 36: "if (std::find(str.begin(), str.end(), str.front()) != str.end()){" - error C2039: 'front' : is not a member of 'std::basic_string<_Elem,_Traits,_Ax>'
Line 21/24: "str += va_arg(arguments, str);" - error C2059: syntax error : ')'
Also, am I headed in the right direction or doing something completely wrong?
va_args
. Here's a man who doesn't play by anyone's rules. – Kerrek SBmain
– Mark Lakata