problem in pushback array element in vector array

Pages: 12
I am trying to add some difference of array values in other vector array. like shown below .

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
int sizeVector = 5;
	vector<int> aValues;
	aValues.resize(sizeVector);

	
	for (vector<double>::size_type i = 0; i < sizeVector; i++)	
	{
		for(int j = 0; j < size; j++)
		{
			if ( frmDiff[j] == 0 )
			{
				aValues.push_back(i) = array[j];
				cout << aValues[i] << endl;
				break;
			}
		}
	}

What i want is , need to add each frmDiff in vector array .
But i get run time error at " aValues.push_back(i) = array[j]; "

could any one please current me if i am doing some thing wrong .
Thanks
Last edited on
push_back adds an element to the end of the vector, increasing the size of the vector by one. It doesn't return a value.

To access an element in the vector by index you can do it the same way you do with arrays.
aValues[i] = array[j];
k thanks .
but my answer is 4 4 4 which is wrong ./

1stly , i do't know that how many values i will get whose array values difference is 0 . so i actually do't know size . I want that it automatically update size when new element is added in aValue . How can i do that ?

2ndly i have array values these 4 , 20 , 3 , 2 , 2 , 2 , 2, whose frmDiff is 0.

I need to add thoe valuse in vector .
but using above posted code . my aValues show me result 4 4 4 . which is wrong .
could you please guide me , thanks
Hi Tina,

You probably don't need to worry about the size, because vector automatically resize themselves. Just keep pushing back values. Resize is handy if know in advance how big the vector needs to be, the compiler reserves quite a lot of space any way.

If you want to find out the size of the vector then you can call the builtin vector::size function. Check out the reference section at the top left of this page - you can see all the stuff related to STL in there.

I am confused as to why you have this in the for loop?
vector<double>::size_type i = 0

Check out the begin & end iterators, and iterators in general, also in the reference section - there are examples for each thing.

Also check out the algorithm section, these functions like find & sort which will work on different STL containers.

Hope this all helps. Good luck
thanks Dear .. :) :)

hy ..

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
int sizeVector = 0;
	vector<int> aValues;
	aValues.resize(sizeVector);

	
	for (vector<double>::size_type i = 0; i < sizeVector; i++)	
	{
		for(int j = 0; j < size; j++)
		{
			if ( frmDiff[j] == 0 )
			{
                                int a = array[j];
				aValues.push_back(a) ;
				cout << aValues[i] << endl;
			}
		}
	}


Hy , my result is 0 0 0 0 . which is not correct .
Amy i doing some thing wrong ?
a values should be 4 20 5 3 2 2 2 2
but i get 0 0 0 0 , its tested it by putting break ponts .
and my data is not pushed in aValues /
please help me thanks
sizeVector is set to zero, and it is also the limit of the for loop, so the program does nothing.

There is nowhere you ask for the values 4, 20 , 5 etc. You need a for loop to put this data into the vector:

1
2
3
4
5
6
unsigned count;
for (count = 0; count < 8; count++) {
   cout << "Next number\n";
   cin >> a;
   aValues.push_back(a) ;
}


You could use a while loop instead, so you can type in a char to end the input.

This is not all of your code - there is no main().

Why do yo have the inner for loop? The push_back will happen sizeVector * size times - is that what you want?

What is your goal - what is the code supposed to do?
ohh sorry wait ..
i have an array and frmDiff array .
I need to get the values of array where frmDiff is 0.

this is a part of my code .

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
 

nt sizeVector = 0;
vector<int> aValues;
aValues.resize(sizeVector);

for(int j = 0; j < size; j++)
	{
		if ( frmDiff[j] == 0 )
		{
			int a = array[j];
			aValue.push_back(a) ;
			cout << aValue[j] << endl;
		}
	}


where size is 47 .
I need to add those array values into vector whose frmDiff is 0.
and then i need to get the minimum value from vector array values .

Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
int sizeVector = 0;
vector<int> aValues;
aValues.resize(sizeVector);

for(int j = 0; j < size; j++)
	{
		if ( frmDiff[j] == 0 )
		{
			int a = array[j];
			aValue.push_back(array[j]) ;
			cout << aValue[j] << endl;
		}
	}

aValue[j] is probably not the same as the position where the value was pushed to. Use a separate loop to print the aValue vector.

Edit: As I said earlier - there is no need to resize the vector.
Last edited on
hy but u did not define the size of aValues. ..
i know push_back it self allocate size in memory .
hy but u did not define the size of aValues. .


There is no need. Just push_back values into it.
Hy ,

If that what you meant ?

1
2
3
4
5
6
7
8
9
vector<int> aValues;
	for(int j = 0; j < size; j++)
	{
		if ( frmDiff[j] == 0 )
		{
			aValues.push_back(j) ;
			cout << aValues[j] << endl;
		}
	}


I get run time run .
I have run time error .
vector expression out of range .
Last edited on
you want array{j] not j, don't you?

I might have to leave it at this for now, it's 3am here. Try some things - see how you get on.
vector<int> aValues;
for(int j = 0; j < size; j++)
{
if ( frmDiff[j] == 0 )
{
aValues.push_back(array[j]);
cout << aValues[j] << endl;
}
}

I got same error.
I got run time error . expression vector subscription out of range .
i doing some thing wrong .

You get an error trying to access aValues[j] when j >= aValues.size(). If you want to print the last element in the vector you can use the back member function to access that element.
cout << aValues.back() << endl;
Last edited on
Oh yar wait .
let me post my complete code .

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
int main()
{
	int  array[47] = { 19 , 11 , 7 ,4 , 4 ,6 , 9, 14, 18 ,20 ,20
	                   , 17 , 13 , 9, 7 , 5, 3, 3, 2, 2 ,2 , 2 , 3, 3, 2, 2, 1,2 , 
					   4, 5,5,4,5,7, 10,11,9,6,5,7,14,20,21,16,11 ,5 , 1 };

	double frmDiff[47];
	int size=sizeof( array) /sizeof(int);      //total size of array/size of arra
	cout<< "size : " << size << endl;

	for(int i = 0; i < size; i++)
	{
		frmDiff[i] = array[i] - array[i+1];
		cout<< " array at index : " <<  i << " : "<< array[i] 
				<< "   =   " << "  diff -->   " << frmDiff[i] << endl;
	}

	cout<< " --------------------------------------------------- " << endl;

	vector<int> aValues;
	for(int j = 0; j < size; j++)
	{
		if ( frmDiff[j] == 0 )
		{
			if( j >= aValues.size() )
			{
				aValues.push_back(array[j]);
				cout << aValues[j] << endl;
			}
		}
	}
}


I am still getting run time error .. out of range .
could u run it & please let me know my problem .
The first loop has a problem in that array[i+1] is out of bounds when i == size - 1.

In the second loop aValues[j] is out of bounds when j >= aValues.size().
Last edited on
How second loop is out of bound .
I did not get .. why j >= aValue.size() wil be out of bound .
j is an index into fromDiff/array. You are putting some of the values from array into aValues but not all of them, therefore j will be larger than aValues.size() once you've skipped an element. The amount by which it is larger will tend to increase the longer the loop goes on.

1
2
3
4
5
6
7
8
    for(int j = 0; j < size; j++)
    {
        if ( frmDiff[j] == 0 )
        {
            aValues.push_back(array[j]) ;
            cout << aValues.back() << endl ;
        }
    }
HY ,


Thanks , i am trying to find minimum values in that aValues ./
but my minimum result is not correct ./
that's my code

1
2
3
4
5
6
7
8
9
10
nt mn = 0;
	mn = aValues[0];
	for(int j = 0; j < aValues.size() ; j++)
    {
		if (aValues[j] < mn) 
		{
			mn = array[j];
		}
    }
	cout<< "mini : " << mn  << endl;



Edited :

when i put that code inside a function and called like this

int mn = miniValus(aValues)

and it returnd me expected result .
but why did not give me expected result when i put the above code inside main function
thanks
Last edited on
Pages: 12