Move items between arrays and vectors

There is some barrier and I want to rearrange the array such that all values < barrier come first and > barrier come last. What I thought to do was evaluate each item one by one and send it to a designated vector. However, I am very inexperienced with vectors so I may be doing it completely wrong. Can you please take a look at this for me.

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
int takeSides(string a[], int n, string barrier)
{
	vector<string> bigger;   //this vector stores values bigger than barrier
	vector<string> smaller;  //this vector stores values smaller than barrier
	vector<string> equal;    //this vector stores values equal to barrier
	int biggerCounter = 0;   //this keeps track of items in bigger
	int smallerCounter = 0;
	int equalCounter = 0;
	for (int i = 0; i < n-1; i++)
	{
		if (a[i] < barrier)               
		{
			smaller.at(smallerCounter) = a[i];         //if the value of array in spot i is less than barrier, send it to smaller
			smallerCounter++;                       //increase the number of values in smaller by 1
		}
		if (a[i] > barrier)
		{
			bigger.at(biggerCounter) = a[i];
			biggerCounter++;
		}
		if (a[i] == barrier)
		{
			equal.at(equalCounter) = a[i];
			equalCounter++;
		}
	}
	int smallerOrEqual = (smallerCounter + equalCounter);    //smallerOrEqual used to separate vectors for drawing data back into array
	for (int j = 0; j < n-1; j++)
	{
		if (j < smallerCounter)                    
		{
			a[j] = smaller.at(j);          //a[j] will take a corresponding value from smaller until all values in smaller are used
		}
		if ((j >= smallerCounter) && (j < smallerOrEqual))
		{
			for (int k = 0; k < equalCounter-1; k++)
			{
				a[j] = equal.at(k);  //a[j] will take a corresponding value from equal until all values in equal are used
			}
		}
		if (j >= smallerOrEqual)
		{
			for (int l = 0; l < biggerCounter-1; l++)
			{
				a[j] = bigger.at(l);   //a[j] will take a corresponding value from bigger until all values in bigger are used
			}
		}
		
	}
	return smallerCounter;  //goal is to return the position of the first non-smaller value
}

debug:
1
2
3
4
5
int main()
{
string a[6] = { 6 strings };
cout << takeSides(a, 6, "a string") << endl;
}
To add a value to a vector, use push_back:
11
12
13
if (a[i] < barrier)               
{
    smaller.push_back(a[i]);

You also don't need those counter variables (e.g. smallerCounter) because you can just get the size of the vector by calling the size() member function on it (e.g. smaller.size()).

You can also use array syntax with vectors to access elements (so smaller[3] returns the element at position 3, just like if smaller was an array).
(Although, using at has the advantage of bounds-checking, so it'll throw an exception if you try to access an out-of-bounds element.)
Why are you deleting your posts after your question is answered? That's rude and selfish.
closed account (iAk3T05o)
I've been seeing that lately and it's irritating. Do you think they'll be anything on the net if everyone deleted their questions, tutorials and articles?
Were you asking "solved, thanks the poster below"?
S/he's done that with the other threads they've started, too. I'm inclined to start reporting people who do this, in the hope that they get their posting privileges revoked.
Last edited on
closed account (iAk3T05o)
It just makes no sense. Why would you be deleting something you asked and yet search the net for answers relating to your questions.
If someone where to search on the net for help on this same topic, he will find it only to see "solved, blah blah blah" and start wondering what the hell you were asking.
I wonder what valid reason s/he had for deleting it.
It just makes no sense. Why would you be deleting something you asked and yet search the net for answers relating to your questions.

Selfishness. Pure, abject selfishness. It's an abuse of the forum.
closed account (iAk3T05o)
And s/he's done it more than once!
Perhaps there could be like a warning system that if you do this twice, your account gets closed but people don't seem to be afraid of that either. There are closed accounts everywhere.
If it was possible to put the email address on probation.
You ask a question, you close your account as soon as you get an answer. You ask a question, you delete the post as soon as you get an answer. Is that normal?
If it's pride then there is something seriously wrong.
I don't really know what motivates it. I wonder if it's students being afraid they'll get caught asking for help?

In any case, it's still selfish - using this forum to get what they want, but being unwilling to contribute anything lasting to it.

Occasionally, I miss the old days when internet access was a privilege, and if you abused it your sysadmin would take away your access...
Topic archived. No new replies allowed.