Hi,

Imagine a vector of structures with each structure containing various variables.

struct Data
{
double x
double y
double z
}

vector<Data> Dataset
vector<Data>::iterator it

//input some data from a file into DataSet

How can I iterate over this vector, comparing say, x at the nth row to the x at the (n-1th) row and the x at the (n-2th) row, beginning at n (where n is Dataset.begin() + 2) and moving one n forward at a time?

Thanks,
TR

Edited 3 Years Ago by toneranger

You've got a clue right here:

where n is Dataset.begin() + 2

Vectors get you random access iterators, so you can also subtract.

So start at Dataset.begin() + 2, increment your iterator while it's not equal to Dataset.end() as usual, and just refer to the iterators (it - 1) and (it - 2) in your loop.

You could also start with three iterators pointint at Dataset.begin(), Dataset.begin() + 1, and Dataset.begin() + 2, and increment them all, using the last one to control the loop. It seems like this might get you a performance improvement, but I'm not 100% sure about that. At the moment, I prefer the first approach; I consider it more readable.

Thanks Gusano79. I wrote up some code, and it compiles but I get a debug assertion failed at runtime:

\VC\include\vector
Line:163

**Expression: ("_Myptr +-Off <= ((_Myvec ** (this->_Getmycont()))->_MyLast&*& etc

Any clues as to what is wrong? Line 163 is commented out in my code, so not sure why it points to there.

Thanks

    int main(int argc, char* argv[]) 
    { 

    if (argc != 2) 
    { 

    std::cerr << 

    "Usage: " << argv[0] << " filename " << std::endl; 

    return EXIT_FAILURE; 
    } 


    PriceList prices; 

    ReadPricesFromFile(argv[1], prices); 
    std::map <std::string, unsigned int> SwingLowMap; 
    std::vector<PriceInfo>::iterator it; 
    it = prices.begin();
    advance (it, 2);



    for (;it!= prices.end();) 
    { 


    // first condition
    if (it->Low > (it-1)->Low) 


  { 
    // another condition
    if ((it-1)->Low <= (it-2)->Low) 


    {

        SwingLowMap.insert(std::pair <std::string, unsigned int> ((it-1)->Date, (it-1)->Time)); 
    } 
  }

    ++it;

    } 


     for (MapIterator iter = SwingLowMap.begin(); iter!=SwingLowMap.end(); iter++) 
    { 
        std::cout << "Date:" << iter->first << std::endl << "Time:" << iter->second << std::endl; 
    } 


    char quitKey;

    std::cout << "Press q to quit" << std::endl;

    std::cin >> quitKey;

    return EXIT_SUCCESS; 
    } 

It looks like the error is coming out of the vector class itself, not your code. Hm. I'm not sure from reading the error message what's going on, but your iterator syntax looks odd.

it is the actual iterator, while *it gets you the object to which the iterator refers.

So you have (for example):

(it-1)->Low

...but I think you mean this:

(*(it - 1)).Low

...unless your definition of PriceList is unusual; I'm assuming it's a fairly thin wrapper around vector<PriceInfo>.

Hi Gusano,

My Pricelist typedef is simple:

typedef std::vector<PriceInfo> PriceList;

I don't think the indirection operator is needed (?) because that kind of code has compiled and worked before when I was comparing variables. For ex, I had done this in the past and it worked:

firstLow= it->Low
++ it

if (it->Low < first low)
etc

I don't think the indirection operator is needed (?) because that kind of code has compiled and worked before when I was comparing variables.

Oh, now that you mention it, I seem to remember that iterators are sometimes implemented as plain old pointers... I'm used to seeing the dereferencing syntax.

So. On my machine, your code builds and runs just fine, after I fill in a few blanks. Can you post the whole thing, including the data file? Just so we're testing the same thing.

Did you check to see if prices had anything in it? What happenes if you put this after line 17 and before line 18?

if(prices.size() < 2)
    cout << "Vector size less than 2!" << endl
else
    cout << "Vector has 2 or more elements!" << endl

Nathan,

You're right, something's up with the vector, it prints 'vector size less than 2!'. Weird coz my code to load the file data into the vector of structures has worked just fine before...

Turned out I forgot to add .txt to the data file at the command prompt... Duh. Thanks much for your help guys, Gusano and Nathan. I'm sure I'll be back later.

This question has already been answered. Start a new discussion instead.