Hello,
I am working with vector arrays for first time, and I encountered a vector returning function in one of my assignments.
Find's job is to return a vector of all the indices of v whose value is sought.
For example, if sought were 5 and v held {5, 47, 3, 5, 5, 5, 101, 5, 5, 17},
then find would return the positions of the six 5's in the array:
{0, 3, 4, 5, 7, 8}
1 2 3 4 5 6 7 8 9
|
vector<unsigned> find(unsigned sought, const vector<unsigned> & v)
{
vector<unsigned> repeated;
for (unsigned i = 0; i < v.size(); i++)
{
if (v[i] == sought);
repeated.push_back(v[i]);
}
return repeated;
|
I also don't know how to output the result I wrote something like
1 2 3 4 5 6 7 8 9 10
|
main(){
show(find(5, Vunsigned));
}
void show(const vector<double> & v)
{
cout << "[" << v.size() << "]" << endl;
for (unsigned i = 0; i < v.size(); i++)
cout << v[i] << " ";
cout << endl;}
|