分别用C++代码实现迭代器 指针 引用
时间: 2023-10-14 17:08:18 浏览: 88
迭代器:
```cpp
#include <iostream>
#include <vector>
int main() {
std::vector<int> vec {1, 2, 3, 4, 5};
std::vector<int>::iterator iter;
for (iter = vec.begin(); iter != vec.end(); iter++) {
std::cout << *iter << " ";
}
std::cout << std::endl;
return 0;
}
```
指针:
```cpp
#include <iostream>
int main() {
int a = 10;
int *p = &a;
std::cout << "a 的值:" << a << std::endl;
std::cout << "a 的地址:" << &a << std::endl;
std::cout << "p 的值:" << p << std::endl;
std::cout << "p 指向的值:" << *p << std::endl;
return 0;
}
```
引用:
```cpp
#include <iostream>
int main() {
int a = 10;
int &ref = a;
std::cout << "a 的值:" << a << std::endl;
std::cout << "a 的地址:" << &a << std::endl;
std::cout << "ref 的值:" << ref << std::endl;
std::cout << "ref 指向的地址:" << &ref << std::endl;
return 0;
}
```
阅读全文