yesterday I spent almost an our while debugging this thing and since then I can't stop thinking about it.C
I tried to implement a 2D matrix with string indexes...
class CSqrMatrix(){
....
void insert(string str){
bool b = map.insert(str,m_size).second;
if (b){
m_size++;
vector<int> ve;
for (int i = 0; i < m_vect.size(); i++)
ve.push_back(m_default);
m_vect.push_back(ve);
for (auto v : m_vect)
v.push_back(m_default);
}
...
map<string,int> map;
vector < vector<int> > m_vect;
int m_default;
int m_size;
};
after some insertions, when I tried to reach an element like
m_vect[0][0] = 8;
I got an invalid write and segfault... and the value of m_vect[0].size()
was 0;
I tried everything, when finally I changed the for each loop to a normal one like
for (int i = 0; i < m_vect.size(); i++){
m_vect[i].push_back(m_default);
the program worked well...
So does that mean, that v
ins't a referecne, but a new copy of the element?
Thanks (there might be typos in the code, I wrote it on the phone...)