如何为txt文件中的每个名称创建一个整数? [英] How can I make a integer for every name in a txt file?

查看:150
本文介绍了如何为txt文件中的每个名称创建一个整数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个文本文件,其内容如下:Bill, 89 Alex, 64 Dan, 29,它代表一些学生的姓名和他们的笔记.
我想按注释升序对它们进行排序,而我的想法是为每个名称创建一个整数,例如:int dan = 29;,以便进一步对它们进行排序,但是我不知道如何从文本中为此名称分配一个整数文件.
希望您能提供帮助,因为这是我第一次编写更复杂的程序.谢谢!

I have a text file with content like this: Bill, 89 Alex, 64 Dan, 29, which represents the names of some students and their note.
I want to sort them ascending by note, and my idea is to make an integer for every name, for example: int dan = 29; so further I can sort them, but I have no idea how to assign a integer for this names from the text file.
Hope you can help because is the first time I make a little bit more complex program. Thank you!

推荐答案

这里的问题是,在C ++中(与在C中一样),您必须在编译时定义所有变量 ,即当您编写您的代码.在您的示例中,您的文本文件具有任意行数,因此编写代码时您实际上并不知道其内容.这就是为什么文件的每一行都不能拥有自己的变量的原因. 因此,如果您需要存储文件每一行的信息,则可以执行此操作.首先,您定义用于存储您的信息的格式(即名称为+整数的字符串).这是您最可能需要struct或类似内容的地方. structclass是在一个结构中聚合不同变量的方法(本质上,它们不只是这些,但这不是重点).如果您写的是这样的话

The problem here is that in C++ (as in C) you have to define all your variables in compile time, i.e. when you write your code. And in your example you have text file with arbitrary count of lines, so you dont really know about it's content when you write code. This is why you cannot have very own variable for every line of your file. So, if you need to store information about every line of your file, you can do this. First, you define format in which your information (i.e. string with name + integer) will be stored. This is the place where you most likely will need struct or something like that. struct and class are the way to agregate diffirent variables in one structure (essentially, they are more than just that, but that's not the point at the moment). If you wrote smth like

struct Data {
  std::string name;
  int ratio;
};

这意味着您刚刚定义了名为Data的数据类型,该数据类型具有字段"name"和"ratio".因此,您可以像这样使用它:

that means that you just defined data type called Data, that have fields "name" and "ratio". So you can use it like this:

Data d;
d.name = "Ben";
d.ratio = 42;

此后,您需要一些实体,该实体可以让您存储许多此Data条目.例如,您可以使用数组.想象一下,您将在此处使用静态数组:

After this, you need some entity that can let you to store many of this Data entries. For example, you could use arrays. Imagine you will use static array here:

const std::size_t lineCount = getLineCount();
Data data_array[lineCount]; //Will not compile, since you cannot use variables as static arrays's size
for( std::size_t i = 0; i < lineCount; ++i ) {
  data_array[i].name = readName();
  data_array[i].ratio = readRatio();
}

如您所见,这里的问题是编写代码时您不知道文本文件的行数.因此,您可以使用以下代码来动态数组:

Problem here, as you can see, is that you cannot know line count of your textfile when you write code. So you can you dynamic arrays with this code:

const std::size_t lineCount = getLineCount();
Data* data_array = new Data[lineCount];
for( std::size_t i = 0; i < lineCount; ++i ) {
  data_array[i].name = readName();
  data_array[i].ratio = readRatio();
}
delete[] data_array;

如果您设法以某种方式设法知道行数(如果您在实际工作之前未完成文件的话),这将是正常的.

This will work, provided you somehow managed to know count of lines if your file before actual work is done, which is not the point usually.

原始动态数组也不太好使用,因为您需要担心该新/删除函数对.因此,实际上是来自Standart模板库的名为容器的类.此类为您提供了仅在其中插入数据并以多种方式处理此数据的方法.这是"map","unordered_map","multimap","vector"等类别,以及以前的答案中的其他类别.其中最容易理解的是矢量,您可以在此处上阅读.其他(例如地图)应该更适合您的任务.

Also raw dynamic arrays are not really nice to use because of that new/delete function pair you have to worry about. So, here in action come classes called containers from Standart Template Library. This classes provides you way to just insert data in it, and operate with this data in many ways. This is the classes "map", "unordered_map", "multimap", "vector" and others from former answers. Easiest of them to understand is vector, and you can read about it here. Others (say'd, map) should fit better to your task.

最后,要对此类数据的数组(或容器)进行排序,可以使用Standart模板库中名为算法的函数.它们提供了按所需方式处理数据的通用方法. 您可以在此处阅读更多信息.

Finally, to sort array (or container) of such data, you can use functions called algorithms from Standart Template Library. They provide generic way to operate on data in the way you need. You can read more here.

不确定我是否需要在这里说更多,探索链接,继续学习,祝你好运!

Not sure I need to say more here, explore links, continue your studies, good luck!

这篇关于如何为txt文件中的每个名称创建一个整数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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