我一直在尝试运行一个简单的矢量代码,我在互联网上看到,以了解矢量的工作原理.
#include <iostream>
#include <vector>
using namespace std;
int main(int argc, char** argv) {
/* Initialize vector of 10 copies of the integer 5 */
vector<int> vectorOne(10,5);
/* run through the vector and display each element, if possible */
for (long index=0; index<20; ++index) {
try {
cout << "Element " << index << ": " << vectorOne.at(index) << endl;
}
catch (exception& e) {
cout << "Element " << index << ": index exceeds vector dimensions." << endl;
} …Run Code Online (Sandbox Code Playgroud)