I have some code like this:

vector<double> Saved;
for(int i = 0; i<10; i++)
{
  if(some condition on i)
      Saved.push_back(i);
}

Then I want to see what order the things were saved... so naturally I do

cout << Saved.at(0) << endl << Saved.at(1) << endl;

But to my surprise they aren't in the right order!! Pretty odd if you ask me...

I would expect the first thing output there to always be less than the second thing... correct??

The actual code follows, but relies on a bunch of stuff so I don't think it will be very helpful:

for(int scancounter = 0; scancounter < NumScans; scancounter++) //alpha loop

	{

		for(int concentriccol = 0; concentriccol < ColsPerScan; concentriccol++) //beta loop

		{
			B.clear();
			V1.clear();
			V2.clear();

			B = LiDARGrid.ConcentricScans.at(scancounter).getColumn(concentriccol);	

		
			double Penalty = CreateComparableVectors(A,B,V1,V2);


			double distance = VectorDistance(V1,V2) + Penalty;


			if(distance < 100) //distance bigger than this is unreliable

			{
				if(distance <= BestDistance)

				{

					BestDistance = distance;

					BestScanPositions.push_back(scancounter);

					BestCols.push_back(concentriccol);

				}
			}
			

		}//end col loop

	}//end scanner loop

	cout << "Input Scan: " << CorrectPosition << " Col: " << CorrectCol << endl

			<< "Matching Scans: " << endl
			<< BestScanPositions.at(0) << " Col: " << BestCols.at(0) << endl
			<< BestScanPositions.at(1) << " Col: " << BestCols.at(1) << endl
			<< "Best Distance: " << BestDistance << endl
			<< "There were " << BestScanPositions.size() << " good matches." << endl << endl;

An example is BestScanPositions.at(0) is 0 and BestScanPositions.at(1) is also 0. Then BestCols.at(0) is 19 and BestCols.at(1) is 16!!! Weird, eh?

Please let me know if I'm doing something wrong..

Thanks,

Dave

Recommended Answers

All 4 Replies

1) when using vectors you can access individual elelements just as you would do with a simple int array B = LiDARGrid.ConcentricScans.[scancounter].getColumn(concentriccol); Otherwise, this works. Your problem is probably something other than push_back

#include <vector>
#include <iostream>
using namespace std;

int main()
{
vector<double> Saved;
for(int i = 0; i<10; i++)
{
    if( (i%2) == 0)
        Saved.push_back(i);
}
vector<double>::iterator it;
for(it = Saved.begin(); it != Saved.end(); it++)
{
    cout << *it << "\n";
       
}


}

1) when using vectors you can access individual elelements just as you would do with a simple int array B = LiDARGrid.ConcentricScans.[scancounter].getColumn(concentriccol);

except that it doesn't check bounds

except that it doesn't check bounds

at() and [] both throw exceptions on out-of-bounds, at least it does with VC++ 2008 Express.

[edit]Nope, I'm wrong. Adding try/catch block the at() will throw and exception, but [] will just simply crash. But this is not relevent if the coder doesn't use try/catch blocks.[/edit]

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.