I have a c++ program currently working with a vector that uses a character and number as its keys. e.g. myvector['A'][1] = line.substr(0,7). But I need it to work as myvector[1][3] = line.substr(0,7) so I can use both keys as numbers.
In my current working code I had this:
std::vector<std::vector<std::string> >myvector;
I thought simply changing the string to integer would work but I get a “Segmentation Fault (core dumped)” or “cannot convert ‘std::basic_string’ to ‘int’ in assignment” error.
std::vector<std::vector<int> >myvector;
I know that error is very vague but I am new to c++ so I do not how to find any other specific command response for an error. I have looked around on the web for a bunch of examples but have sadly not been able to compile any of them. Any assistance would be greatly appreciated; thank you for your time.
If I am somehow using indexes out of my range here is how I am inputting my indexes.
myvector[rn].resize(100);
std::ifstream fin(argv[3]);
std::string line;
int rn = 0;
int rln = 0;
while( getline(fin, line) ) {
rn = 0;
while(rn < line.length()/7){
myvector[rn][rln] = line.substr (rn*7,7);
rn++;
}
rln++;
}
When I output “line.substr (rn*7,7)” the results are exactly as expected I just cannot set this variable into my vector. Also rn ends up being around 10+ and rln ends at 6.
In C++, string is in double quote
"test"not in single code'test', try:Also With std::vector you have no key concept, actually you are calling access
operator[].