I am using VC++ 2013.I'm trying to access the index of the vector where it shows vector subscript out of range.My code is given below:
std::string str="1,2,3, 4 , 0.00000 , ";
vector<string>veclist;
veclist=(tokenize(str,","));
//Now the veclist has a size of 6.But when i am trying to access it through;
long num=stol(veclist.at(4));
The code crashes and shows vector subscript out of range.I don't know why the code crashes and what's the error in this!!!
The code crashes and shows vector subscript out of range.I don't know why the code crashes and what's the error in this!!!
The message tells you all.
It crashes since you have no rights to access veclist.at(4)
. Simply you have not enough elements in veclist
or veclist
is empty.
Check the number elements in veclist
by veclist.size()
after executing (tokenize(str,","));
Edit:
You may have different problem then reported. Your str
tokens may not be liked by the stol
function. That can cause the crash.
Consider this test program. I have written tokenize_split
which should match yours.
#include <vector>
#include <string>
#include <string.h>
#include <sstream>
#include<iostream>
using namespace std;
std::vector<std::string> tokenize_split(const std::string &s, char delim) {
std::stringstream ss(s);
std::string item;
std::vector<string> tokens;
while (getline(ss, item, delim)) {
tokens.push_back(item);
}
return tokens;
}
int main(void)
{
std::string str1="1 2 3 4 5 6";
std::string str2="1,2,3, 4 , 0.00000 , "; // Your string
std::string str3="1,2,3,4,5,6";
vector<string> veclist;
long num;
veclist = tokenize_split(str3, ',');
std::cout << "Size= " << veclist.size() << endl;
std::cout << "Token= " << "<" << veclist.at(4) << ">" << endl;
num = std::stol(veclist.at(4));
std::cout << "Number " << num << endl << endl;
veclist = tokenize_split(str2, ',');
std::cout << "Size= " << veclist.size() << endl;
std::cout << "Token= " << "<" << veclist.at(4) << ">" << endl;
num = std::stol(veclist.at(4));
std::cout << "Number " << num << endl << endl;
veclist = tokenize_split(str1, ' ');
std::cout << "Size= " << veclist.size() << endl;
std::cout << "Token= " << "<" << veclist.at(4) << ">" << endl;
num = std::stol(veclist.at(4));
std::cout << "Number " << num << endl << endl;
veclist = tokenize_split(str2, ' ');
std::cout << "Size= " << veclist.size() << endl;
std::cout << "Token= " << "<" << veclist.at(4) << ">" << endl;
num = std::stol(veclist.at(4));
std::cout << "Number " << num << endl;
return 0;
}
Test:
Size= 6
Token= <5>
Number 5
Size= 6
Token= < 0.00000 >
Number 0
Size= 6
Token= <5>
Number 5
Size= 5
Token= <,>
terminate called after throwing an instance of 'std::invalid_argument'
what(): stol
Aborted
Program crashes since your string str2
does not contain a valid number that could be successfully converted to long
by the stol
function. The token ','
will cause the abort.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.