C ++ - access to the vector

I have a std::vector that contains my own class, and I need to access its functions and voids.

 class A { private: int var; vector<string> vec; public: void setVar(int i) { var = i; } void setVec(vector<string> a) { vec = a; } }; 

I also have return functions, but I did not print them. And I included all the necessary files.

 int main() { vector<A> vec; for (int i = 0; i < 10; i++) { A tmp; tmp.setVar(i); vec.push_back(tmp); } for (int i = 0; i < 10; i++) { vector<string> tmp; tmp.push_back("1"); tmp.push_back("2"); tmp.push_back("3"); vec.at(i).setVec(tmp); //Works sometimes or causes error std::out_of_range vec[i].setVec(tmp); //Crashes the whole programm } } 

So, how do I set these variables since vector initialized?

I use g ++ and this is not actual code, because my actual code is dirty.

 Error for vec.at(i).setVec(tmp); Error is: terminate called after throwing an instance of 'std::out_of_range' what(): vector::_M_range_check 
+2
source share
4 answers

Which compiler are you using? I am really very surprised that std::vector<std::string> tmp("1","2","3"); even compiles!

Honestly, I have no idea what he calls, but I'm sure he is not doing what you expect, what I think:

 std::vector<std::string> tmp; tmp.push_back("1"); tmp.push_back("2"); tmp.push_back("3"); 

For writing boost.assign and C ++ 0x provides the type of container initialization you tried to achieve.

+2
source

The code you posted will not compile yet (no tmp declared in the first loop), so let me explain in general what is going on.

You mentioned two types of errors:

 vec.at(i).setVec(tmp); //Works sometimes or causes error std::out_of_range 

The at function tries to be safe - it first checks the length of the vector, then returns an element of the given index or throws std::out_of_range if the vector does not contain an element of such an index.

Second case:

 vec[i].setVec(tmp); //Crashes the whole programm 

The operator [] behaves in the same way as the at() function for vectors, but it is not "safe" because it does not fulfill any restrictions. Therefore, if you try to access the 4th element from a 3-element, you just get access to some random place in your memory (maybe, for example, another, unrelated variable, maybe something else). If you're lucky, then your program will crash. If you're out of luck, you will have memory corruption issues and very strange errors that are hard to find.

The solution to your problem:

a) Replace vec[i] with vec.at(i) - work the same way (well, a tiny bit is slower, but you wonโ€™t feel it), and you are safe.

b) Then: Look at all the places when you really do this vector search and stop at each second for a second and think: "How big is this vector at this moment? Am I sure that an element of this index exists?"

You will probably find your mistake quickly.

+2
source

I tried my code in VS2010 and could not reproduce the problem you described. Of course, passing a vector by value is not a good idea, it should be a reference to a constant, but it cannot cause such an error. Given that "std :: out_of_range" is raised, the most likely reason is the lack of an element with that index in your vector. For testing, you can check if i < vec.size() before accessing vec[i] (or vec.at(i) )

+1
source

I found a problem. It was in a function that handled the size of the vector. Apparently, the vector was empty. Invalid error. Thanks everyone for the answers.

0
source

All Articles