#include <iostream>
#include <vector>
#include <string>
using namespace std;
int main()
{
vector<string> a;
a.push_back("1 1 2 4");
a.push_back("2 3 3 3");
a.push_back("2 2 3 5");
a.push_back("3 3 3 3");
a.push_back("1 2 3 4");
for (int i=0;i<a.size();i++)
for(int j=0;j<a[i].length();j++)
cout<<a[i].at[j];
return 0;
}
Hi,when I run the code above,there is an error as below:
error C2109: subscript requires array or pointer type
Please help me and tell me why,thanks!
atwith round brackets:at(j)atat all. I've never found a situation where its semantics are appropriate (especially withstd::string).at()is useful and is needed.std::stringthat worries you ? I usually recommend usingat()rather than[]to protect against out of bounds aspects.atis the solution. But beware in actual applications. If a bounds check error is an actual error (violation of a precondition), you don't want to unwind the stack (which may be corrupted); you want to get out of the process executing as little code as possible. If the internal state is corrupted, even executing a destructor could make things even worse.