vector array related question

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;}
repeated.push_back(v[i]); You need vector of indices. So you should do: repeated.push_back( i );

I also don't know how to output the result I wrote something like
Well, it looks fine. What exactly do you have problem with?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include "stdafx.h"
#include <vector>
#include <iostream>

using namespace std;

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(i);
		}
	}
	return repeated;
}

void show(const vector<unsigned> &v)
{
	cout << "[" << v.size() << "]" << endl;
	for (unsigned i = 0; i < v.size(); i++)
		cout << v[i] << " ";
	cout << endl;
}

int main()
{
	vector<unsigned> Vunsigned = { 5, 47, 3, 5, 5, 5, 101, 5, 5, 17 };
	//vector<unsigned> output = ;
	show(find(5, Vunsigned));

	int x;
	cin >> x;
}
line 6, remove the semi-colon.
Thank you problem solved.
Topic archived. No new replies allowed.