how can i change the element of vector.
for example the vector is : 3,2,5,8,7
i want to change the second number 2 to 6
Printable View
how can i change the element of vector.
for example the vector is : 3,2,5,8,7
i want to change the second number 2 to 6
See this article on std::vector.Code:std::vector<int> arr;
arr.push_back(3);
arr.push_back(2);
arr.push_back(5);
arr.push_back(8);
arr.push_back(7);
// method 1
arr[1] = 6;
// method 2
std::vector<int>::iterator pos = arr.begin() + 1;
*pos = 6;
Instead of accessing elements by its index you´d better use the replace function (in case you want to replace all occurences...):
Regards,Code:std::vector<int> arr;
arr.push_back(3);
arr.push_back(2);
arr.push_back(5);
arr.push_back(8);
arr.push_back(7);
// replace all occurences
replace( arr.begin(), arr.end(), 2,6 );
// replace first occurence of 2
std::vector<int> pos = find( arr.begin(), arr.end(), 2 );
if( pos != arr.end() )
{
*pos = 6;
}
Guido
Don't you think that depends on the logic of the program? What if he wants to change the third occurence of 2 with 6? In that case none of your samples (which are good OTOH) can help.Quote:
Instead of accessing elements by its index you´d better use the replace function (in case you want to replace all occurences...):
I agree with that... but somehow you have to know which element you want to replace. I wanted to show how you can replace items regardless of their position.Quote:
Originally Posted by cilu
However, if you want to replace an item that requires certain criterions you can use replace_if and provide a predicate that checks these criterions.
Regards,
Guido
thanks
but i can`t find the problem with my code
there is no error when debugging, but it prompt error when runningCode:void CDelta::Decompose( int m, int n)
{
int index = 0;
if( m==0 )
CreateU( index++ );
else
for( int i = m; i >= 1; i-- )
{
if( n == 0 || i <= m_vectTemp[n-1] )
{
if( m_vectTemp.size() > n )
m_vectTemp[n] = i;
else
m_vectTemp.push_back( i );
Decompose( m-i, n+1 );
}
}
/*
int i;
if(m==0)
show(n);
else
for(i=m;i>=1;i--)
if(n==0||i<=a[n-1])
{
a[n]=i;
decompose(m-i,n+1);
}
*/
}
What is the error that it prompts?
the system show a dialog which prompt:
there is something wrong with vector.exe, it must be colsed...........
please send the report of problem to Microsoft............
( i just translated it to english )
Does the vector contain some elements when you call Decompose? Can you post the code where it calls Decompose the first time?
i just inited it as vector< int > m_vectTempbut when i push some zeros in it,
the system prompted error also
the error was prompted when the first time to run to m_vectTemp[n] = i
(n == 0 )
well if n==0 then the second condition in your if statement would be m_vectTemp[0-1] which clearly is an error.
if( n == 0 || i <= m_vectTemp[n-1] )
if n==0 the second condition will not be judged
IMHO the second condition is still judged.
Oh my god!
the error happend not in the function of Decompose but CrateU
:cry:
Ok I was wrong. I tried it and the second condition was not judged. My bad.:oQuote:
Originally Posted by Sarevok
:eek: ok you should have just used the debug tools in the first place.Quote:
Originally Posted by blacksource