列出文本文件中的项目 [英] Listing out the items from a text file

查看:42
本文介绍了列出文本文件中的项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要按升序或降序列出记事本中的项目。

我该怎么做呢。需要一些代码

你的帮助表示赞赏



i need to list out the items from the notepad in ascending or descending order.
how do i go about doing that. need some codes
ur help is appreciated

int main(int argc, char *argv[]);
{
    std::ifstream output_file("stockdatabase.txt");
    std::string value;
    std::vector<std::string xmlns:std="#unknown"> getItemID;
    if (output_file)
    {
        while (!output_file.eof())
        {
            std::getline(output_file, value, '\n');
            getItemID.push_back(value);
            std::sort(getItemID.begin(), AscendingOrderSorter());
        }
        for (int x = 0; x < stockVectorTemp.size(); x++)
        {
            cout << "[" << x+1 << "]\t" << stockVectorTemp[x].getItemID()
            << "\t" << stockVectorTemp[x].getTotalQty()
            << "\t\t" << stockVectorTemp[x].getItemDesc() << endl;
        }
        for (int y =0; y>stockVectorTemp.size(); y--)
        {
            cout << "[" << y-1 << "]\t" << stockVectorTemp[y].getItemID()
            << "\t" << stockVectorTemp[y].getTotalQty()
            << "\t\t" << stockVectorTemp[y].getItemDesc() << endl;
        }

推荐答案

只需将文件中的所有项目读取到' std :: vector< std :: string xmlns :std =#unknown> '并为此向量调用' std :: sort '函数。



PS你可以使用' std :: sort '函数的谓词来实现你自己的排序逻辑。



祝你好运!
Just read all items from file to 'std::vector<std::string xmlns:std="#unknown">' and call 'std::sort' function to this vector.

P.S. You could use a predicate for 'std::sort' function to implement your own sorting logic.

Good luck!


#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include <algorithm>


struct AscendingOrderSorter
{
  bool operator()(const std::string& lhs, const std::string& rhs)
  {
    return lhs < rhs;
  }
};

struct DecsendingOrderSorter
{
  bool operator()(const std::string& lhs, const std::string& rhs)
  {
    return lhs > rhs;
  }
};

struct RowPrinter
{
  void operator()(const std::string& value)
  {
    std::cout << value << "\n";
  }
};

int main(int argc, char *argv[])
{
  std::ifstream input_file("D:\\123.txt");
  std::string value;
  std::vector<std::string> file_data;

  if (input_file)
  {
    while (!input_file.eof())
    {
      std::getline(input_file, value, '\n');
      file_data.push_back(value);
    }
  }
  std::cout << "Before sort: \n";
  std::sort(file_data.begin(), file_data.end(), AscendingOrderSorter());
  // now file_data is sorted.
  std::cout << "After sort: \n";
  std::for_each(file_data.begin(), file_data.end(), RowPrinter());
}


这篇关于列出文本文件中的项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆