C++文件读写以及vector的遍历

写一个文本,然后编写一个程序,打开文本然后将每一个字读取到一个vector对象中。遍历vector,将内容显示到cout。然后利用泛型算法sort(),对文字进行排序。

#include <algorithm>
sort(container.begin(), contaner.end())

再将排序后的结果输出入另一个文件。

#include <iostream>
#include <fstream> //读取文件的头文件
#include <vector> 
#include <algorithm>
using namespace std; 
int main()
{
    ifstream infile("new1.txt"); //验证是否可以打开文件
    if (! infile) //当不能打开时满足条件
    {
        cerr << "please check the file\n"; //cerr和cout一样
    }                                      //但对输出结果无缓存(bufferred)
    ofstream outfile("new2.txt");          //立即显示与终端
    if(! outfile)
    {
        cerr << "please check the file\n";
    }

    string word;                
    vector<string> msg;
    while(infile >> word)    //将文本中的内容读取到字符串word中,知道录取到最后一个字符,返回值为0
    {
        msg.push_back(word); //vector.push_back() 相当于将字符串的内容分配给vector中。类比数组
    }

     unsigned int i;   //为什么是unsigned? 因为i需要与vector.size()作比较,后者为unsigned类型
    cout << "unsorted msg: \n";

    for(i = 0; i < msg.size(); i++) //遍历vector msg
    {
        cout << msg[i] << ' ';
    }
    cout << endl;

    sort(msg.begin(), msg.end( )); //进行sort()操作

    /*outfile << "sorted msg: \n";  //写入outfile

    for(i = 0; i < msg.size(); i++) //遍历sort()后的vector msg,再将其写进outfile
    {
        outfile << msg[i] << ' '; 
    }
    outfile << endl;*/
	
	cout << "sorted msg: \n";  //这里选择直接输出看结果(与写如outfile中的内容相同)

    for(i = 0; i < msg.size(); i++) //遍历sort()后的vector msg
    {
        cout << msg[i] << ' '; 
    }
    cout << endl;
	
    system("pause");
    return 0;
}
  • 0
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
回答: 在Linux下,可以使用dirent.h头文件的相关函数来遍历文件夹。下面是一个示例代码,可以用来遍历文件夹并获取文件名: ```cpp #include <iostream> #include <vector> #include <dirent.h> #include <cstring> void GetFileNames(const std::string& path, std::vector<std::string>& filenames) { DIR *pDir; struct dirent* ptr; if(!(pDir = opendir(path.c_str()))){ std::cout << "Folder doesn't Exist!" << std::endl; return; } while((ptr = readdir(pDir))!=0) { if (strcmp(ptr->d_name, ".") != 0 && strcmp(ptr->d_name, "..") != 0){ filenames.push_back(path + "/" + ptr->d_name); } } closedir(pDir); } int main() { std::string folderPath = "./folder"; std::vector<std::string> filenames; GetFileNames(folderPath, filenames); for(const auto& filename : filenames) { std::cout << filename << std::endl; } return 0; } ``` 这段代码使用了`opendir`函数打开文件夹,然后使用`readdir`函数遍历文件夹的文件。通过判断文件名是否为"."或".."来排除当前目录和上级目录。最后使用`closedir`函数关闭文件夹。你可以根据自己的需求修改代码的文件夹路径和处理文件的逻辑。 #### 引用[.reference_title] - *1* *3* [windows/Linux C++遍历文件夹里的所有文件并进行读写](https://blog.csdn.net/baidu_35231778/article/details/113849845)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [Linux 下 C++ 遍历文件夹下的文件名](https://blog.csdn.net/weixin_38419133/article/details/103561532)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值