本文介绍: 如图所示,直接打一组数字也出不来,必须后面加个非类型的(非int)的值,才能异常输出,这是我一个疑惑的地方,为什么要这么输出呢?输入数字也不排,混合输入也不排(经过实验) 然后我想这代码可能是错的,并没有输出排序后的功能。这个代码,神了,我找了好久类似的,也用gpt跑了,根本跑不出来,只会让人无限输入,写啥也不返回。思考从字符串入手,找到了一个排序字符串,按空格输出的代码,觉得很好。输出结果后发现,其并不需要输入非类型,但是也不排序。我混乱了,难道必须要输入异常才能返回吗。
#include<iostream>
#include<iterator>
#include<algorithm>
#include<vector>
#include<string>

using namespace std;
int main(){
istream_iterator<string> is(cin);
istream_iterator<string> eof;

vector<string> text;
copy(is,eof,back_inserter(text));

sort(text.begin(),text.end());

ostream_iterator<string> os(cout," ");
copy(text.begin(),text.end(),os);
}

这个代码,神了,我找了好久类似的,也用gpt跑了,根本跑不出来,只会让人无限输入,写啥也不返回

于是找了个int的

#include<iostream>
#include<iterator>
#include<algorithm>
#include<vector>
#include<string>

using namespace std;

int main() {
std::vector<int> numbers;

// 从标准输入流中读取数据,并将其添加到vector中
std::istream_iterator<int> inputIterator(std::cin);
std::istream_iterator<int> endIterator;
std::copy(inputIterator, endIterator, std::back_inserter(numbers));
sort(numbers.begin(), numbers.end());
// 打印vector中的数据
std::ostream_iterator<int> outputIterator(std::cout, " ");
std::copy(numbers.begin(), numbers.end(), outputIterator);

return 0;
}
//这个程序应

如图所示,直接打一组数字也出不来,必须后面加个非类型的(非int)的值,才能异常输出,这是我一个疑惑的地方,为什么要这么输出呢??

然后我想是不是代码有问题

修改了以下第一个代码,将string 改为Int,其他原封不动

#include<iostream>
#include<iterator>
#include<algorithm>
#include<vector>
#include<string>

using namespace std;
int main(){
istream_iterator<int> is(cin);
istream_iterator<int> eof;

vector<int> text;
copy(is,eof,back_inserter(text));

sort(text.begin(),text.end());

ostream_iterator<int> os(cout," ");
copy(text.begin(),text.end(),os);
}

如图所示,必须输一个非类型才能打出来

我混乱了,难道必须要输入异常才能返回吗

思考之后,用gpt输入回车程序结束,用c++语言表示

#include <iostream>
#include <iterator>
#include <algorithm>
#include <vector>
#include <string>

int main() {
    std::vector<std::string> text;

    while (true) {
        std::string input;
        std::getline(std::cin, input);

        // 检查输入是否为空(用户按下回车键)
        if (input.empty()) {
            break;
        }

        text.push_back(input);
    }

    // 对字符串进行排序
    std::sort(text.begin(), text.end());

    // 打印排序后的字符串
    for (const auto& str : text) {
        std::cout << str << " ";
    }

    return 0;
}

输出结果后发现,其并不需要输入非类型,但是也不排序

输入数字也不排,混合输入也不排(经过实验)        然后我想这代码可能是错的,并没有输出排序后的功能

#include <iostream>
#include <algorithm>
#include <string>

int main() {
    // 输入字符串
    std::cout << "Enter a string: ";
    std::string input;
    std::getline(std::cin, input);

    // 对字符串进行排序
    std::sort(input.begin(), input.end());

    // 输出排序后的字符串,用空格隔开
    std::cout << "Sorted string: ";
    for (char c : input) {
        std::cout << c << ' ';
    }
    std::cout << std::endl;

    return 0;
}

思考从字符串入手,找到了一个排序字符串,按空格输出的代码,觉得很好

#include <iostream>
#include <algorithm>
#include <string>

int main() {
    // 输入字符串
    std::cout << "Enter a string: ";
    std::string input;
    std::getline(std::cin, input);

    // 对字符串进行排序
    std::sort(input.begin(), input.end());

    // 输出排序后的字符串,用空格隔开
    std::cout << "Sorted string: ";
    for (char c : input) {
        std::cout << c << ' ';
    }
    std::cout << std::endl;

    return 0;
}

但这个输出的是单个字符,于是加以修改

#include <iostream>
#include <algorithm>
#include <vector>

int main() {
    // 存储字符串
    std::vector<std::string> fruits;
 while (true) {
        std::cout << "Enter a fruit (or enter an empty string to finish): ";
        std::string input;
        std::getline(std::cin, input);

        // 检查输入是否为空,如果是则退出循环
        if (input.empty()) {
            break;
        }

        // 将输入的字符串添加到向量中
        fruits.push_back(input);
    }

	
    // 对字符串进行排序
    std::sort(fruits.begin(), fruits.end());

    // 输出排序后的字符串
    std::cout << "Sorted fruits: ";
    for (const auto& fruit : fruits) {
        std::cout << fruit << " ";
    }
    std::cout << std::endl;

    return 0;
}

(加入两个gpt出来的思考过程)

如果您想对字符串 “apple”, “banana”, “orange”, 和 “cherry” 进行排序,可以将它们存储在一个 std::vector<std::string> 中,然后使用 std::sort 进行排序。以下是相应的C++代码: 

 

#include <iostream>
#include <algorithm>
#include <vector>

int main() {
    // 存储字符串
    std::vector<std::string> fruits = {"apple", "banana", "orange", "cherry"};

    // 对字符串进行排序
    std::sort(fruits.begin(), fruits.end());

    // 输出排序后的字符串
    std::cout << "Sorted fruits: ";
    for (const auto& fruit : fruits) {
        std::cout << fruit << " ";
    }
    std::cout << std::endl;

    return 0;
}

这个程序首先将字符串存储在 std::vector<std::string> 中,然后使用 std::sort 对向量进行排序,最后输出排序后的结果。在这个例子中,输出应该是 “apple banana cherry orange”。请注意,上述程序假设您要按照字典序对这些水果进行排序。如果您想按照其他方式排序(例如,按照字符串的长度),您可以在调用 std::sort 时提供一个比较函数。例如,按照字符串长度降序排序的示例:

#include <iostream>
#include <algorithm>
#include <vector>

// 自定义比较函数,按照字符串长度降序排序
bool compareByLength(const std::string& a, const std::string& b) {
    return a.length() > b.length();
}

int main() {
    // 存储字符串
    std::vector<std::string> fruits = {"apple", "banana", "orange", "cherry"};

    // 对字符串按照长度降序进行排序
    std::sort(fruits.begin(), fruits.end(), compareByLength);

    // 输出排序后的字符串
    std::cout << "Sorted fruits by length: ";
    for (const auto& fruit : fruits) {
        std::cout << fruit << " ";
    }
    std::cout << std::endl;

    return 0;
}

结果如下: 

依然又回到第一个函数了,按照上面思路修改,依然报错

 可能是迭代器没有empty类型的原因,那么想用回车将其输出的可能性就pass了。

iostream iterator代码

#include <iostream>
#include <algorithm>
#include <vector>
#include <iterator>
#include <string>

int main() {
    // 从输入流中读取字符串,直到遇到空字符串
    std::cout << "Enter fruits (enter an empty string to finish): ";
    std::istream_iterator<std::string> inputIterator(std::cin);
    std::istream_iterator<std::string> endIterator;
    std::vector<std::string> fruits(inputIterator, endIterator);

    // 对字符串进行排序
    std::sort(fruits.begin(), fruits.end());

    // 输出排序后的字符串到输出流
    std::cout << "Sorted fruits: ";
    std::ostream_iterator<std::string> outputIterator(std::cout, " ");
    std::copy(fruits.begin(), fruits.end(), outputIterator);
    std::cout << std::endl;

    return 0;
}

如图所示,毫无反应,我开始还想这会不会是string独有特性,但前面也用string顺利排序了,可以成功。但这个将Int修改再输入异常值就可以跳出,string我试不出来异常值。估计这是 iostream iterator的string的独有特性

原文地址:https://blog.csdn.net/weixin_61847358/article/details/135745051

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。

如若转载,请注明出处:http://www.7code.cn/show_60681.html

如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱:suwngjj01@126.com进行投诉反馈,一经查实,立即删除!

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注