I have been facing problems with the programs crashing, due to accessing wrong index (out of range index).
Just now checked that vector::at is the best method to access the individual elements of a vector list. As it throws an 'out of range' exception.
Unfortunately, I have written all my programs until now using vector::operator[]. Cant see an error handling procedure for it. Is it possible that I can make it throw an exception, instead of getting the program crashed?
I think that some compiler manufacturers put error handling on the vector::operator[] but it is not specified either way in the Standard. The best thing to do is check the index is in range yourself before using it.
I think that some compiler manufacturers put error handling on the vector::operator[] but it is not specified either way in the Standard. The best thing to do is check the index is in range yourself before using it.
I always have done like that.
My boss who is a Python programmer is using SWIG interface to invoke C++ functions from Python (as its faster). I do the coding in C++. In the beginning I was doing all the checks, but my boss asked me to remove all the checks to speed up further. And now after two years, he wants some kindof error handling and all the codes now need to be modified.
I have a doubt with usage of 'at' function of vector in the case of multi-dimensional arrays. How do I use it?
1 2 3 4 5
vector< vector<int> > My2DVec(3,vector<int> (5)); /* A 3*5 2D Matrix */
cout<<My2DVec[2][3]; //Classic way using operator[]
cout<<(My2DVec.at(2)).at(3); //Using "at" operator.. does it work? Any other elegant way?? As I also got 3D vectors to deal with :(
No, the chained calls to at() are the only way. Unfortunately the nesting of STL containers provides for some unwieldy syntax, an issue that many people agree exists with the STL. If you want to eliminate that, you'll need to write a facade that provides operator() as the element-access operator (because operator[] only accepts a single parameter).
1 2 3 4
struct ThreeDMatrix {
// Lots of stuff
intoperator()( int x, int y, int z ) const;
};
Using a #define like that seems like a terrible idea. If you need to refactor the code then refactor the code correctly. Adding a #define like that is going to be a nightmare and it will create more problems then it will solve. If you use the .at() call then you need to add try..catch blocks to handle the exception. Or you can manually check the indices prior to accessing the operator[]. Even then you still might have to throw or return an error from a function somehow. If you are required to do the checks then it probably doesn't make much difference either way. I'm surprised that your boss told you to remove the checks due to performance constraints and now wants you to add it back. The original suggestion seemed rather silly. I don't see how verifying an array index is going to slow your program down considerably.
Ofcourse I did not use the #define method, as things could get really messy. I just manually changed the array access with .at() calls. Probably another 3-4 days work I guess! Got nothing else to do, so rather 'clean' it up.
My boss isnt a programmer, so he did not understand. I tried to explain but at that time he did not want it. Now he realises the mistake. We all learn from mistakes.
Ok, I have one that works (in simple cases)...if you run it through a few times. I would watch out for the way it's looking for (-something-greedily-)[ for the 2+ runs. It's worth a quick shot, though.