[Solved] Size of C++ vector is invalid


From your non-understable question, I can guess that you want the vector to treat the [] operator as “edit if it is exist or create if it is not”. Some thing like the behavior of [] operator in std::map for example..

You simply can not. It was not designed like this.

BTW, you may do something like this: (It is bad idea. Do not.)

int& funky_operator(std::vector<int>& vec, const std::size_t index){
    if(index<vec.size()){
        vec.resize(index+1);
    }
    return vec[index];
}
int main(){
    std::vector<int> intVec;
    intVec.reserve(5);
    funky_operator(intVec,0) = 1;
    std::cout << "intVec Size: " << intVec.size();
}

Again, this is stupid code. Do not use in real life. However, it is what you want.

solved Size of C++ vector is invalid