CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 16
  1. #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. #2
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    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.
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  3. #3
    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 10:43 AM.
    - Guido

  4. #4
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    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.
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

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

    Re: how to change the element of vector

    Quote 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

  6. #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. #7
    Join Date
    Aug 2005
    Location
    The Matrix
    Posts
    159

    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. */

  8. #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. #9
    Join Date
    Aug 2005
    Location
    The Matrix
    Posts
    159

    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. */

  10. #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. #11
    Join Date
    Aug 2005
    Location
    The Matrix
    Posts
    159

    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. */

  12. #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. #13
    Join Date
    Aug 2005
    Location
    The Matrix
    Posts
    159

    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. */

  14. #14

    Re: how to change the element of vector

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

  15. #15
    Join Date
    Aug 2005
    Location
    The Matrix
    Posts
    159

    Re: how to change the element of vector

    Quote 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. */

Page 1 of 2 12 LastLast

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured