vector keeps different elements inside and outside of the function. Why?

I am trying to solve euler question 419 (source: https://projecteuler.net/problem=419)

I am keeping the numbers in a vector called testVec. I'm sharing code here. What I am confused about is testVec returns true values in called function but after I call the vector outside of the function it returns first two element only. Why is that?

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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124

#include <iostream>
#include <stdio.h>
#include <vector>
#include <cmath>

std::vector<int> form_1;
std::vector<int> form_2;
std::vector<int> testVec;

void showVec(std::vector<int> vec)
{
	//
	for (int i = 0; i < vec.size(); i++)
	{
		//
		std::cout << vec[i] << std::endl;
	}
}

void resFin(int start, int stop, std::vector<int> vec)
{
	//
	for (int i = 0; i < vec.size(); i++)
	{
		//
		if (i == 0)
		{
			//
			form_1.push_back(vec[0]);
			std::cout << "form_1 pushed " << vec[0] << std::endl;
		}
		else
		{
			//
			if (i != vec.size() - 1)
			{
				//
				if (vec[i] == vec[i - 1])
				{
					//
					form_1.push_back(vec[i]);
					std::cout << "form_1 pushed " << vec[i] << std::endl;
				}
				else
				{
					//
					form_2.push_back(form_1.size());
					form_2.push_back(vec[i - 1]);
					form_1.clear();
					form_1.push_back(vec[i]);
				}
			}
			else
			{
				//
				if (vec[i] == vec[i - 1])
				{
					//
					form_1.push_back(vec[i]);
					std::cout << "form_1 pushed " << vec[i] << std::endl;
					form_2.push_back(form_1.size());
					std::cout << "form_2 pushed " << form_1.size() << std::endl;
					form_2.push_back(vec[i - 1]);
					std::cout << "form_2 pushed " << vec[i - 1] << std::endl;
					form_1.clear();
				}
				else
				{
					//
					form_2.push_back(form_1.size());
					std::cout << "form_2 pushed " << form_1.size() << std::endl;
					form_2.push_back(vec[i - 1]);
					std::cout << "form_2 pushed " << vec[i - 1] << std::endl;
					form_2.push_back(1);
					std::cout << "form_2 pushed " << 1 << std::endl;
					form_2.push_back(vec[i]);
					std::cout << "form_2 pushed " << vec[i] << std::endl;
					form_1.clear();
				}
			}
		}
	}
	vec.clear();
	for (int k = 0; k < form_2.size(); k++)
	{
		//
		vec.push_back(form_2[k]);
		std::cout << "vec pushed " << form_2[k] << std::endl;
	}
	//showVec(vec);
	if (start + 1 != stop)
	{
		//
		form_1.clear();
		form_2.clear();
		std::cout << "recursed to " << start + 1 << std::endl;
		resFin(start + 1, stop, vec);
	}
	else
	{
		//
		showVec(vec);
	}
}

void stepFind(int stop, std::vector<int> vec)
{
	//
	resFin(1, stop, vec);
}

int main()
{
	//
	testVec.push_back(1);
	testVec.push_back(1);
	stepFind(7, testVec);
	std::cout << "now I'm being a dick!" << std::endl;
	showVec(testVec);
	getchar();
	return 0;
}

Last edited on
All of your functions are passing the vector parameter by value. What that means is a copy of the entire vector is made and passed to the function. What you need is to pass the parameter by reference. In that case no copying is done (therefore it is more efficient and uses less resources) as the vector inside the function is the actual vector which was passed.

However, there is a danger. When you pass the actual vector like this, it may accidentally be changed when you don't want it to be. For example when you print out the contents of the vector, you don't want any changes to occur. To guard against this, use the keyword const which tells both the reader and the compiler of your intentions. The compiler will then give an error message if your code in some way tries to make unwanted changes.

1
2
// vec passed by value, copy is made
void showVec(std::vector<int> vec) 


1
2
// vec passed by reference, it may be changed
void resFin(int start, int stop, std::vector<int>& vec)  


1
2
// vec passed by const reference, it may not be changed
void showVec(const std::vector<int>& vec) 


See Arguments passed by value and by reference in tutorial page:
http://www.cplusplus.com/doc/tutorial/functions/

Last edited on
Topic archived. No new replies allowed.