查找并排序列表
所以我有一个像这样的 2 列列表:
2834 2
2934 1
2712 1
2834 3
2889 1
2659 1
2934 1
2760 1
2173 1
2834 1
2760 1
2834 2
第一列是商品 ID,第二列是购买的商品的数量,每个商品的价格位于另一个文件中。我需要做的是找到订购的每件商品的总数量,将其乘以价格,然后显示按收入排名前五的商品。 例如,上面的 2834 被订购了 8 次,它的价格是 2 美元,所以 2834 的总收入是 16 美元。如何找到一个项目的所有出现次数(数量),将其数量和 ID 号保存在某处,并对其他项目执行相同的操作,而不重复前一个项目。有人告诉我地图在这里很有用,但我不知道如何使用它们。
So I have a list of 2 columns like this:
2834 2
2934 1
2712 1
2834 3
2889 1
2659 1
2934 1
2760 1
2173 1
2834 1
2760 1
2834 2
The first column is an item ID and the second column is the quantity of that item that is purchased, the price of each item is in another file. What I need to do is find the total quantity of each item ordered, multiply it by its price and then display the top five items by revenue.
So for example, above 2834 is ordered 8 times, the price of it is $2 so the total revenue for 2834 is $16. How do I find all the occurrences (quantity) of one item, save it's quantity and ID number somewhere, and do the same for the other items without repeating for a previous item. I was told maps would be useful here but I don't know how to use them.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
使用一个映射,其中键是 ID 号,值是数量:
循环遍历列表并更新映射中的相应条目。您可以在地图上使用
[]
运算符来访问与给定键对应的值。例如,如果您有一个 ID 和数据中的数量,您可以执行以下操作:这无需初始化地图即可工作,因为如果
[]
运算符要求输入尚未包含的条目存在于地图中,它会自动插入一个默认值,并且int
的默认值为零。现在,您可以浏览地图并找到所有商品的价格。地图的迭代器为您提供一个
对
,其中第一个元素是地图的键,第二个元素是该键的值。您可以编写一个像这样的循环来打印地图的内容:如果您找到每个商品的价格并将其乘以
it->second
,您就会得到您想要的东西。Use a map where the key is the ID number and the value is the quantity:
Loop through your list and update the corresponding entry in the map. You can use the
[]
operator on the map to access the value corresponding to a given key. For example, if you had an ID and the quantity from your data, you could do this:That works without having to initialize the map because if the
[]
operator asks for an entry that doesn't yet exist in the map, it's automatically inserted with a default value, and the default value forint
is zero.Now, you can go through the map and find the prices of all your items. The map's iterator gives you a
pair
, where the first element is the map's key and the second element is the value for that key. You can write a loop like this to print the map's contents:If you find the price for each item and multiply it by
it->second
, you'll have what you're looking for.std::map 会很有用 - 它基本上是一个按键索引的数组。在您的情况下,项目 ID 将是关键。通常,您只需将一个元素放入映射中:
例如,假设 readFromFile() 填充 id 和 qty:
第一次通过时,[] 将创建该元素。要迭代最终列表,您可以在映射上使用标准迭代器,就像其他 STL 容器一样。
A std::map would be useful - it's basically an array that's indexed by keys. In your case, the item ID would be the key. Normally you just put an element in a map with:
So for example, assuming readFromFile() fills in id and qty:
The first time through, the [] will create the element. To iterate through the final list you can use standard iterators on a map, just like other STL containers.
我将定义一个结构
product
来保存有关每个项目的信息。该结构将保存ID
、数量
和价格
。您可以创建一个
std::map
,其中索引是项目的ID
。您将读取价格文件,并针对每一行,使用该
ID
和价格创建一个新的product
,然后使用将其添加到您的地图中ID
作为索引。然后,您将读入销售文件,对于每一行,通过
ID
从地图中引用获取product
,并将数字添加到quantity
。最后,您将迭代地图,并写出每个
ID
以及订购的数量
乘以价格
。我理解正确吗?
I would define a struct
product
to hold the information about each item. The struct would hold theID
,quantity
, andprice
.You would make a
std::map<int, product>
where the index is the item'sID
.You would read in the price file, and for each line, make a new
product
with thatID
and price, and then add it to your map, with theID
as the index.You would then read in the sales file, and for each line, get the
product
by reference from the map by theID
, and add the number to thequantity
.Finally, you would iterate through the map, and write out each
ID
, and thequantity
ordered multiplied by theprice
.Did I understand correctly?
这看起来就像建立了一个关系数据库。
一张表将包含 [Item_ID, Quantity] 等记录。
另一个表将包含 [Item_ID, Price] 等记录。
如果您选择接受,您的任务是创建两个表,每个表一个文件。使用第一个表中的 Item_ID 查找数量。使用第二个表中的 Item_ID 来查找价格。将这两个变量相乘即可确定 Item_ID 的总成本:
首先研究
std::map
数据结构。另请在 Stack Overflow 中搜索“while getline c++”以了解如何读取文件。This looks like a relational database set up.
One table will have records like [Item_ID, Quantity].
Another table will have records like [Item_ID, Price].
Your task, should you choose to accept it, is to create two tables one for each file. Use the Item_ID in the first table to find the quantity. Use the Item_ID into the second table to find the price. Multiply these two variables to determine the total cost for the Item_ID:
Start by researching the
std::map
data structure. Also search Stack Overflow for "while getline c++" to find out how to read files.