编写一程序,让用户自由地从键盘上输入整数,直到输入数字0时结束。把用户输入的数据保存在模板类Vector<int>定义的对象中,并对用户输入的所有数进行排序输出
时间: 2024-02-06 13:10:33 浏览: 71
下面是一个实现上述功能的C++程序:
```c++
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
vector<int> nums; // 定义一个整型向量
// 循环输入整数,直到输入0时结束
int num;
while (cin >> num && num != 0) {
nums.push_back(num); // 将输入的数添加到向量中
}
sort(nums.begin(), nums.end()); // 对向量中的数进行排序
// 输出排好序的数
for (int i = 0; i < nums.size(); i++) {
cout << nums[i] << " ";
}
cout << endl;
return 0;
}
```
在上面的程序中,我们使用了C++标准库中的vector容器来保存用户输入的数字,并使用了algorithm库中的sort函数对其进行排序。程序通过循环读取用户输入的数字,并将非零的数添加到vector中,最后对vector中的数字进行排序并输出。
相关问题
编写一程序,让用户自由地从键盘上输入整数,直到输入数字 0 时 结束。把用户输入的数据保存在模板类 vector 定义的对象中,并对 用户输入的所有数进行排序输出。在向显示器进行输出时,要求用以 下 3 种方式实现:
好的,我来为您编写这个程序。
```c++
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
template <typename T>
void sortAndPrint(const vector<T>& vec, int option) {
vector<T> sortedVec(vec);
sort(sortedVec.begin(), sortedVec.end());
switch (option) {
case 1:
for (auto& elem : sortedVec) {
cout << elem << " ";
}
cout << endl;
break;
case 2:
for (auto it = sortedVec.rbegin(); it != sortedVec.rend(); ++it) {
cout << *it << " ";
}
cout << endl;
break;
case 3:
for (int i = 0; i < sortedVec.size(); ++i) {
if (i % 2 == 0) {
cout << sortedVec[i] << " ";
} else {
cout << sortedVec[i] << endl;
}
}
if (sortedVec.size() % 2 != 0) {
cout << endl;
}
break;
default:
cout << "Option not supported!" << endl;
break;
}
}
int main() {
vector<int> nums;
int num;
do {
cout << "Please enter an integer (0 to quit): ";
cin >> num;
if (num != 0) {
nums.push_back(num);
}
} while (num != 0);
cout << "Sorted in ascending order: ";
sortAndPrint(nums, 1);
cout << "Sorted in descending order: ";
sortAndPrint(nums, 2);
cout << "Sorted in alternating order: ";
sortAndPrint(nums, 3);
return 0;
}
```
该程序使用了模板类 vector 来保存用户输入的整数,并通过函数 sortAndPrint 对用户输入的整数进行排序和输出。该函数的第二个参数表示输出方式,1 表示升序,2 表示降序,3 表示交替输出。
程序演示了用户输入整数的过程,直到用户输入数字 0 才结束。然后,程序分别以三种方式对用户输入的所有数进行排序输出。
请您试着运行一下该程序,看看是否符合您的要求。
用c++语言编写一程序,让用户自由地从键盘上输入整数,直到输入数字0时 结束。把用户输入的数据保存在模板类vector定义的对象中,并对 用户输入的所有数进行排序输出。在向显示器进行输出时,要求用以 下3种方式实现:仅使用vector的相关成员函数,不使用迭代器进行输出;使用迭代器自己编程输出;使用STL提供的通用算法for_each进行输出。
以下是用C++编写的程序:
```cpp
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
template<class T>
void printVector(vector<T>& v) {
for (int i = 0; i < v.size(); i++) {
cout << v[i] << " ";
}
cout << endl;
}
template<class T>
void printVectorWithIterator(vector<T>& v) {
typename vector<T>::iterator it;
for (it = v.begin(); it != v.end(); it++) {
cout << *it << " ";
}
cout << endl;
}
template<class T>
void printVectorWithForEach(vector<T>& v) {
for_each(v.begin(), v.end(), [](T n) {cout << n << " "; });
cout << endl;
}
int main() {
vector<int> v;
int n;
do {
cin >> n;
if (n != 0) {
v.push_back(n);
}
} while (n != 0);
sort(v.begin(), v.end());
cout << "Output with vector member functions: ";
printVector(v);
cout << "Output with iterator: ";
printVectorWithIterator(v);
cout << "Output with for_each: ";
printVectorWithForEach(v);
return 0;
}
```
首先,我们使用do-while循环从键盘上读取整数,并将它们存储在vector中。当用户输入0时,循环结束。
然后,我们使用vector的sort函数对所有输入的数字进行排序。
最后,我们分别使用三种方法输出vector中的所有数字:使用vector的相关成员函数、使用迭代器自己编程输出和使用STL提供的通用算法for_each输出。
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231044937.png)
![-](https://img-home.csdnimg.cn/images/20241231044937.png)
![-](https://img-home.csdnimg.cn/images/20241231044937.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![-](https://img-home.csdnimg.cn/images/20241231044937.png)
![-](https://img-home.csdnimg.cn/images/20241231044937.png)