how to change the element of vector
 CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com

# Thread: how to change the element of vector

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

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;```

3. Senior Member
Join Date
Nov 2006
Location
Essen, Germany
Posts
1,344

## 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 09:43 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.

5. Senior Member
Join Date
Nov 2006
Location
Essen, Germany
Posts
1,344

## 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

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

7. ## Re: how to change the element of vector

What is the error that it prompts?

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 )

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?

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 )

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.

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

13. ## Re: how to change the element of vector

IMHO the second condition is still judged.

14. ## Re: how to change the element of vector

Oh my god!
the error happend not in the function of Decompose but CrateU

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.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•