#include <iostream>
#include <vector>
using namespace std;
int main()
{
//定义了一个空vector<int>对象
vector<int> str;
//动态的添加100个元素,从1-100.
for(vector<int>::size_type i = 0; i != 100; ++i)
str.push_back(i+1);//因为是从0开始的所以让它+1
//用迭代器,输出每个元素.
for(vector<int>::iterator s = str.begin(); s != str.end(); ++s)
cout << *s << endl;
//访问任意的元素.
int a;
const b = -1, c = 100;
cout << "请输入 0 - 99 的元素数 ";
cin >> a;
if(a > b && a < c)
{
cout << "str中第" << a << "个元素的值是" << str[a] << endl;
}
else
{
cout << "请您按要求输入!!!" << endl;
}
//给任意元素赋值.
cout << "请输入 0 - 99 的元素数 ";
cin >> a;
if(a > b && a < c)
{
int f = 0;
cout << "请输入你想要的结果 ";
cin >> f;
str[a] = f;
cout << "第" << a << "个元素的结果现在是: "
<< str[a] << endl;
}
else
{
cout << "请您按要求输入!!!" << endl;
}
system("pause");
return 0;
}
上面是一些用法,自己去想吧!
我也是刚学~~
或许还有更好的方法
参考资料:http://zhidao.baidu.com/question/67730187.html