Dangerous reference !

This is a discussion on Dangerous reference ! within the C++ Programming forums, part of the General Programming Boards category; Hi everyone, I was a solving a problem of changing one vector in a way of removing all the elements ...

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    8

    Dangerous reference !

    Hi everyone, I was a solving a problem of changing one vector in a way of removing all the elements of the vector that the other vector has. And unfortunately it doesn't work. Here is the code :

    Code:
    #include<iostream>
    #include<vector>
    #include<string>
    using namespace std;
    template <typename type>
    void function(vector<type>&a,vector<type>&b){
           for(int i=0;i<a.size();i++){
            int k(0);
            for(int j=0;j<b.size();j++){
                if(a[i]==b[j])
                k=1;
            }
            if(k==1){
            for(int p(i);p<a.size()-1;p++){
                a[p]=a[p+1];
            }
            a.resize(a.size()-1);
            }
        }
    }
    int main(){
        int n;
        cout<<"How many words ?";
        cin>>n;
        cin.ignore(1000,'\n');
        vector<string>s(n);
        for(int i=0;i<n;i++){
            getline(cin,s[i]);
                }
        int b;
        cout<<"How many words now?";
        cin>>b;
        cin.ignore(1000,'\n');
        vector<string>j(b);
        for(int u(0);u<b;u++){
            getline(cin,j[u]);
        }
        function(s,j);
        cout<<endl;
        for(int i=0;i<s.size();i++){
            cout<<s[i]<<endl;
        }
       return 0;
    }
    Last edited by CornedBee; 03-30-2011 at 03:31 PM. Reason: Fixed code tags

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Perhaps you could edit the code to fix code tags and the indentation.

    Anyway, your algorithm is probably skipping values when two consequtive items need to be removed. That's because, after you erase an item, you increment the counter anyway, even though the next item to check is at the same index.

    Suppose you want to remove B and C:

    Code:
    ABCDE
     ^
     i
    
    Next iteration:
    ACDE
      ^
      i
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,295
    So essentially you want to perform a set difference on the two vectors.
    Are the vectors both sorted? If so, there's a std library algorithm that will make it a lot more efficient.

    Code tags ... you're doing it wrong.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  4. #4
    Registered User
    Join Date
    Feb 2011
    Posts
    8
    Thank you very much. You are completely right, it skips... So i put i-- after resize and it works now.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. C OpenGL Compiler Error?
    By Matt3000 in forum C Programming
    Replies: 12
    Last Post: 07-07-2006, 04:42 PM
  5. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 09:18 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21