4 Replies - 1329 Views - Last Post: 11 November 2010 - 07:03 AM Rate Topic: -----

#1 Guest_heidik*


Reputation:

where to increment vector iterator

Posted 10 November 2010 - 08:59 AM

I am trying to find out the duplicates in a vector and then display the two vector elements to show that these are the elemenets that were identical (based on some condition). Could anyone please tell me at which point do I have increment itFirst or make itFirst equal to itCurrent in case the second IF is not true or the third IF is not true or the 4th IF is not true. I have tried the itFirst in while loop (i-e matching each itCurrent with all itFirst) but it didnt work.

std::vector<block>::const_iterator itFirst   = my_file_as_blocks.begin();	// inner loop
        std::vector<block>::const_iterator itCurrent = my_file_as_blocks.begin();	// outer loop
        std::vector<block>::const_iterator itEnd     = my_file_as_blocks.end();

        for ( ; itCurrent != itEnd ; ++itCurrent )
        {
	    // a for/while for itFirst

            if ( itFirst != itCurrent )
            {
		// if order IDs match
		if ( ((itFirst->id1 == itCurrent->id1) || (itFirst->id1 == itCurrent->id2) || (itFirst->id2 == itCurrent->id1) || (itFirst->id2 == itCurrent->id2))
			&& ((itCurrent->id1 == itFirst->id1) || (itCurrent->id1 == itFirst->id2) || (itCurrent->id2 == itFirst->id1) || (itCurrent->id2 == itFirst->id2)))
                {
			if( (itFirst->last_line.find("TIME-DIFF") != std::string::npos )) // if the line stored in last elements of my_file_as_blocks
			{										  // contains "TIME-DIFF"
				// only those blocks where time is not the same for "2nd-ORD-TO-WASH-TIME-DIFF"
				if(itFirst->time_diff != itCurrent->time_diff)
				{
				    	std::cout << "Found a duplicate:" << std::endl;
				} // end of IF for wash_time_diff not-equal comparison

			} // end of IF for existence of "2nd-ORD-TO-WASH-TIME-DIFF"
                } // end of IF for comparing order IDs
            } // end of if ( itFirst != itCurrent )
        } // end of for ( ; itCurrent != itEnd ; ++itCurrent )


example my_file_as_blocks
-------------------------
123
456
789
012
345
123
567
789
123


Is This A Good Question/Topic? 0

Replies To: where to increment vector iterator

#2 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: where to increment vector iterator

Posted 10 November 2010 - 02:47 PM

ok so I have no idea what you are really asking.

But lets try to simplify things a little.

If I have a vector of structs and I want to remove duplicates. In this case I have defined equality a little loosely (this would find [999, 999] == [999, 888] since lhs.id1 == rhs.id1 and lhs.id2 == rhs.id1 -- and I think your conditions are probably a little more strict).

#include <vector>
#include <iostream>

using namespace std;

struct DataStruct {
    int id1, id2;
};

DataStruct data[] = {{123, 345}, {543, 123}, {876, 345}, {543, 123}, {345, 123}, {123, 543}, {123, 123}};

//Operator defined to easily determine equality
bool operator==(const DataStruct& lhs, const DataStruct& rhs) {
    return (lhs.id1 == rhs.id1 || lhs.id1 == rhs.id2) && (lhs.id2 == rhs.id2 || lhs.id2 == rhs.id1);
}


//operator defined so that we can easily output a record
ostream& operator<<(ostream& out, const DataStruct& rhs) {
    return out << "[" << rhs.id1 << ", " << rhs.id2 << "]";
}

int main() {
    vector<DataStruct> vect;
    //load the data into a vector:
    int size = sizeof(data)/sizeof(*data);
    cout << "importing data: " << endl;
    for(int i = 0; i < size; ++i) {
        vect.push_back(data[i]);
        cout << data[i] << endl;
    }
    cout << "remove duplicates: " << endl;
    vector<DataStruct> result;
    for(vector<DataStruct>::const_iterator instance = vect.begin(); instance != vect.end(); ++instance) {
        bool alreadyAdded = false;
        //we only need to check to see if the current value already exits in our list of items...
        for(vector<DataStruct>::const_iterator check = result.begin(); check!= result.end(); ++check) {
            //This test is done using the operator defined above since *instance and *check are of type DataStruct
            if (*instance == *check) { alreadyAdded = true; break; }
        }
        if (!alreadyAdded) {
            cout << *instance << endl;
            //when we first find an item we need to add it to the results
            result.push_back(*instance);
        }
    }

    return 0;
}

Was This Post Helpful? 0
  • +
  • -

#3 heidiK  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 32
  • Joined: 23-October 10

Re: where to increment vector iterator

Posted 11 November 2010 - 04:11 AM

Thanks NickDMax. I am actually reading data from a file into vectors and just trying to determine the duplicates and not remove them i-e "this" is a duplicate of "this" block based on similar ordeIDs. A block starts from the line which has the word "BREACH" in it and it ends with the line "2nd-ORD-TO-WASH-TIME-DIFF" in it. If the first and last line of any block do not consist of these words then those line would not be considered a block.

The data block from the file is something like this:

BREACH:0:40:GE:20100701-10:38:50.375
0:1:ORDER ID:0000F9F5
0:2:ORDER ID:0000F9DA
0:1:TRDR:DAF
0:2:TRDR:DAF
0:0:TRADE CROSSING IDS:1yoo0qv1y9nfbb,10fqfw119twrdg
0:1:OrderReceive,01.07.2010 10:38:09.984,0G508Q090,0000F9F5,A,15,0,15,0,115
0:1:OrderReceive,01.07.2010 10:30:36.903,0G508Q090,0000F9F5,A,15,0,15,0,115
0:2:OrderReceive,01.07.2010 10:23:58.390,0G508Q086,0000F9DA,A,15,0,15,0,70
0:2:OrderReceive,01.07.2010 10:16:25.340,0G508Q086,0000F9DA,A,15,0,15,0,70
0:1:CME,20100701,10:31:17.293,DAF,GE,201009,FUT,XGDAF,0G508Q090,b2dmw1pstc1o,Leg Fill,0000F9F5,B,00000,11.5,4,99.34,20100701,10:31:21
0:1:CME,20100701,10:31:17.293,DAF,GE,201012,FUT,XGDAF,0G508Q090,1yoo0qv1y9nfbb,Leg Fill,0000F9F5,S,00000,11.5,4,99.225,20100701,10:31:21
0:2:CME,20100701,10:31:17.293,DAF,GE,201012,FUT,XGDAF,0G508Q086,10fqfw119twrdg,Leg Fill,0000F9DA,B,00000,7,4,99.225,20100701,10:31:21
0:2:CME,20100701,10:31:17.293,DAF,GE,201103,FUT,XGDAF,0G508Q086,ja9dnxa3wemc,Leg Fill,0000F9DA,S,00000,7,4,99.155,20100701,10:31:21
0:0:WASH-ORD-TIME-DIFF,851.563
0:0:2nd-ORD-TO-WASH-TIME-DIFF,493.472



where the '0' before the first colon is the line ID. The next line ID would be '1' and so on...

If a block has a duplicate then I have to add a line to the parent block after the two order IDs 'ORDER ID' which would be like this:

lineID:0:Order Group:1:0



where 'lineID' is the line ID of that block. '0' and 'Order Group' should be written as it is. '1' that comes after 'Order Group' would indicate the block ID and '0' at the end would determine duplicate ID i-e '0' is the first duplicate of block '1'. If there are many duplicates of that block (i-e block ID '1' in this case) then the second duplicate would be '1' and so on. For example if a second duplicate of that block is found then the line added to the block after the Order IDs would be:

lineID:0:Order Group:1:1



if another duplicate found:

lineID:0:Order Group:1:2



and so on...

and if another block is found which has duplicates then the line added would look like:

lineID:0:Order Group:2:0



i-e the number after the Order Group would now be '2' and '0' after the '2' means the first duplicate of the second block.

The struct I have is:

struct block
{
	std::string lineID;	// alert number
    	std::string id1;	// order ID 1
	std::string id2;	// order ID 2
    	std::string last_line;	// line having "2nd-ORD-TO-WASH-TIME-DIFF,71.843" where the string after the ',' is the time and can be any 
                                // combination of numbers, above is just an example.
	std::string wash_time_diff;	// time difference between the blocks having similar order IDs and its last having the string
                                        // 2nd-ORD-TO-WASH-TIME-DIFF in it
};

// order should match in any direction/way
if ((lhs.id1 == rhs.id1 || lhs.id1 == rhs.id2) && (lhs.id2 == rhs.id2 || lhs.id2 == rhs.id1) && lhs.last_line == rhs.last_line))



I have done a part of it i-e finding the blocks (not duplicates) and storing them in vector and now I have to determine the duplicate blocks (the point where I am stuck) and then adding the line "lineID:0:Order Group:2:0". Finding duplicates also works but not properly, it some times skips some duplicates.

Could you please help me. Can you modify your code according to the task that I have been assigned.

Thanks

and yes a block would be considered duplicates if the Order IDs of those blocks match.

I think I am getting some where :-p. Let me make it sure if my code is working exactly the way it should or not. Else would need you help :-p
Was This Post Helpful? 0
  • +
  • -

#4 heidiK  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 32
  • Joined: 23-October 10

Re: where to increment vector iterator

Posted 11 November 2010 - 04:32 AM

NOOO it didnt work :(
Was This Post Helpful? 0
  • +
  • -

#5 heidiK  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 32
  • Joined: 23-October 10

Re: where to increment vector iterator

Posted 11 November 2010 - 07:03 AM

done :P
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1