Contrary to what the title implies, my real problem lie in returning a vector pointer, and then use it.
Here's what I tried:
#include <vector>
#include <string>
#include <cstring> //? Needed?
#define R_OK 0
#define R_ERROR 1
using namespace std;
vector<string>* tester(){
vector<string> v;
v.push_back("Lolzer");
vector<string> *ptr = &v;
return ptr;
}
int main(){
vector<string>*ptr = tester();
cout<<ptr->at(0);
return R_OK;
}
It compiles, and runs, but as soon as I try to access the vector, the compiler says it's out of bounds (probably because the vector it is pointing to is empty)
Feel free to answer any of these questions :D
- Anyone know why this doesn't work?
- Anyone have a link explaining how to point to vectors correctly?
'cause I've seen something like vector<string*> *ptr as well, and it looks confusing. - Advice about vectors?
- Suggestions on how to further improve the code, giving the same result, but in a clear, clean and understandable (and perhaps, fast) way?