-
December 4th, 2006, 09:27 AM
#1
how to change the element of vector
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
-
December 4th, 2006, 09:31 AM
#2
Re: how to change the element of 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;
See this article on std::vector.
-
December 4th, 2006, 10:34 AM
#3
Re: how to change the element of vector
Instead of accessing elements by its index you´d better use the replace function (in case you want to replace all occurences...):
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;
}
Regards,
Guido
Last edited by GNiewerth; December 4th, 2006 at 10:43 AM.
- Guido
-
December 4th, 2006, 10:50 AM
#4
Re: how to change the element of vector
Instead of accessing elements by its index you´d better use the replace function (in case you want to replace all occurences...):
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.
-
December 4th, 2006, 10:57 AM
#5
Re: how to change the element of vector
Originally Posted by cilu
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.
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.
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
- Guido
-
December 4th, 2006, 09:26 PM
#6
Re: how to change the element of vector
thanks
but i can`t find the problem with my code
Code:
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);
}
*/
}
there is no error when debugging, but it prompt error when running
-
December 4th, 2006, 09:40 PM
#7
Re: how to change the element of vector
What is the error that it prompts?
/** The only stupid question is the one you never ask. */
-
December 4th, 2006, 10:16 PM
#8
Re: how to change the element of vector
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 )
-
December 4th, 2006, 11:55 PM
#9
Re: how to change the element of vector
Does the vector contain some elements when you call Decompose? Can you post the code where it calls Decompose the first time?
/** The only stupid question is the one you never ask. */
-
December 5th, 2006, 12:42 AM
#10
Re: how to change the element of vector
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 )
-
December 5th, 2006, 12:59 AM
#11
Re: how to change the element of vector
well if n==0 then the second condition in your if statement would be m_vectTemp[0-1] which clearly is an error.
/** The only stupid question is the one you never ask. */
-
December 5th, 2006, 01:21 AM
#12
Re: how to change the element of vector
if( n == 0 || i <= m_vectTemp[n-1] )
if n==0 the second condition will not be judged
-
December 5th, 2006, 01:25 AM
#13
Re: how to change the element of vector
IMHO the second condition is still judged.
/** The only stupid question is the one you never ask. */
-
December 5th, 2006, 01:27 AM
#14
Re: how to change the element of vector
Oh my god!
the error happend not in the function of Decompose but CrateU
-
December 5th, 2006, 01:32 AM
#15
Re: how to change the element of vector
Originally Posted by Sarevok
IMHO the second condition is still judged.
Ok I was wrong. I tried it and the second condition was not judged. My bad.
/** The only stupid question is the one you never ask. */
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|