<C++ Primer_5th>习题_3.31
来源:互联网 发布:ubuntu修改hosts翻墙 编辑:程序博客网 时间:2024/06/06 03:55
//编写一个程序,定义一个含有10个int的数组,令每个元素的值就是其所在位置的值#include<iostream>using namespace std;int main(){const int sz = 10;int a[sz];//遍历数组元素并赋值for (int i = 0; i < sz; ++i){a[i] = i;}//输出数组中的全部元素cout << "数组中的元素依次为:";for (auto c : a){cout <<c << " ";}cout << endl;system("pause");return 0;}//将上面创建的数组拷贝给另外一个数组。利用vector重写程序,实现类似功能//利用数组来拷贝#include<iostream>using namespace std;int main(){const int sz = 10;int a[sz], b[sz];//利用for循环为数组赋值for (int i = 0; i < sz; ++i)a[i] = i;for (int j = 0; j < sz; ++j)b[j] = a[j];//利用范围for循环输出数组的全部元素cout << "数组中的元素依次为: ";for (auto c : b)cout << c << " ";cout << endl;system("pause");return 0;}//利用vector实现拷贝#include<iostream>#include<vector>using namespace std;int main(){const int sz = 10;vector<int> v_int, v_int2;for (int i = 0; i < sz; ++i)v_int.push_back(i);for (int j = 0; j < sz; ++j)v_int2.push_back(v_int[j]);//输出vector对象中的元素cout << "输出的元素依次为: ";for (auto c : v_int2)cout << c << " ";cout << endl;system("pause");return 0;}
阅读全文
0 0
- <C++ Primer_5th>习题_3.31
- <C++ Primer_5th>习题_3.2
- <C++ Primer_5th>习题_3.3
- <C++ Primer_5th>习题_3.4
- <C++ Primer_5th>习题_3.5
- <C++ Primer_5th>习题_3.6
- <C++ Primer_5th>习题_3.10
- <C++ Primer_5th>习题_3.14
- <C++ Primer_5th>习题_3.16
- <C++ Primer_5th>习题_3.17
- <C++ Primer_5th>习题_3.20
- <C++ Primer_5th>习题_3.22
- <C++ Primer_5th>习题_3.23
- <C++ Primer_5th>习题_3.24
- <C++ Primer_5th>习题_3.25
- <C++ Primer_5th>习题_3.35
- <C++ Primer_5th>习题_3.36
- <C++ Primer_5th>习题_1.3
- LinkList_Stack(链式栈)
- 第2篇 SercureCRT使用root远程登录ubuntu16.04
- <C++ Primer_5th>习题_3.24
- CodeForces
- <C++ Primer_5th>习题_3.25
- <C++ Primer_5th>习题_3.31
- 3 文本编辑器vim
- 基于Excel的QR二维码生成工具——原理及算法详解(之七)
- <C++ Primer_5th>习题_3.35
- POJ—487-3279
- <C++ Primer_5th>习题_3.36
- QT问题记录之EnterEvent事件无效
- HDU5514 Frogs
- android开发笔记汇总(一)