本文主要是介绍《C++ Primer》课后练习4.32, 4.33,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
4.32
编写程序用int型数组初始化vector对象。
#include<iostream>
#include<string>
#include<cstring>
#include<vector>using namespace std;int main(){const size_t arr_size = 8;int int_arr[arr_size];cout << "Enter " << arr_size << " numbers:" << endl;for(size_t ix = 0; ix != arr_size; ++ix){cin >> int_arr[ix];}vector<int> ivec(int_arr, int_arr + arr_size);return 0;
}
4.33
编写程序把int型vector复制给int型数组。
#include<iostream>
#include<string>
#include<cstring>
#include<vector>using namespace std;int main(){/*const size_t arr_size = 8;int int_arr[arr_size];cout << "Enter " << arr_size << " numbers:" << endl;for(size_t ix = 0; ix != arr_size; ++ix){cin >> int_arr[ix];}vector<int> ivec(int_arr, int_arr + arr_size);*/vector<int> ivec;int ival;cout << "Enter numbers: (Ctrl+Z to end)" << endl;while(cin >> ival)ivec.push_back(ival);int *parr = new int[ivec.size()];size_t ix = 0;for(vector<int>::iterator iter = ivec.begin(); iter != ivec.end(); ++iter, ++ix)parr[ix] = *iter;delete[] parr;return 0;
}
这篇关于《C++ Primer》课后练习4.32, 4.33的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!