比较 c++ 中的字符
Comparing char in c++
这是我从中获取数据的文本文件
10
wood 8
gold 7
silver 5
gold 9
wood 1
silver 1
silver 9
wood 3
gold 5
wood 7
我应该找到同名的商品并添加它们的所有数量,所以最终结果应该是木材=19;黄金=21;白银=15。这就是我到目前为止所做的
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ifstream read("data.txt");
int n;
read >> n;
char name[10][n]; // 10 symbols are given for items name
int amount[n];
for(int i=0; i<n; i++)
{
read.ignore(80, 'n');
read.get(name[i], 10);
read >> amount[i];
}
for(int i=0; i<n; i++)
{
for(int d=1; d<n; d++)
{
if(name[i]==name[d] && i!=d)
{
}
}
}
return 1;
}
到目前为止的问题是,name[i]==name[d]
甚至没有反应,例如name[i]="wood"
和name[d]="wood"
在C++中,我们倾向于使用 std::string
而不是 char[]
。第一个具有相等运算符重载,因此您的代码应该可以工作。对于后者,您需要strcmp()
才能实现目标。
现在你的代码可以这样(我使用了 std::vector,但你可以使用字符串数组,但我不推荐它(:
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;
int main()
{
ifstream infile("data.txt");
int n;
infile >> n;
vector<string> name(n);
int amount[n], i = 0;
while (infile >> name[i] >> amount[i])
{
cout << name[i] << " " << amount[i] << endl;
i++;
}
// do your logic
return 0;
}
顺便说一下,您可以使用 std::pair
,使您的代码更具可读性,其中第一个成员是名称,第二个成员是数量。
与您的问题无关,main()
倾向于在一切正常时return 0;
,而您返回 1。
PS:这是一个工作示例:
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <utility>
using namespace std;
int main()
{
ifstream infile("data.txt");
int n;
infile >> n;
vector<string> name(n);
int amount[n], i = 0;
while (infile >> name[i] >> amount[i])
{
// cout << name[i] << " " << amount[i] << endl;
i++;
}
vector< pair<string, int> > result;
bool found;
for(int i = 0; i < name.size(); ++i)
{
found = false;
for(int j = 0; j < result.size(); ++j)
{
if(name[i] == result[j].first)
{
result[j].second += amount[i];
found = true;
}
}
if(!found)
{
result.push_back({name[i], amount[i]});
}
}
cout << "RESULTS:n";
for(int i = 0; i < result.size(); ++i)
cout << result[i].first << " " << result[i].second << endl;
return 0;
}
输出:
Georgioss-MacBook-Pro:~ gsamaras$ g++ -Wall -std=c++0x main.cpp
Georgioss-MacBook-Pro:~ gsamaras$ ./a.out
RESULTS:
wood 19
gold 21
silver 15
好的,gcc 知道接受它,但 C++ 中不支持可变长度数组,所以这一行:
char name[10][n]; // 10 symbols are given for items name
不符合规定,至少应该发出警告。
处理维度仅在运行时已知的数组C++方法是使用 std::vector
。
但是您真正的问题是原始 char 数组和指向 char 的指针都没有覆盖的==
运算符(数组或指针是不可能的(,因此在您的name[i]==name[d]
中,您实际上是在比较地址,因为数组在表达式中使用时衰减到指向其第一个元素的指针。所以你的测试与if (&name[i][0] == &name[d][0)
相同,不能给出预期的结果。
您可以使用 strcmp
来比较以 null 结尾的字符数组(也称为 C 字符串(,或者更好地使用具有覆盖==
运算符的std::string
。
的 char[] == 运算符是比较指针值,而不是字符串比较值。 即您正在比较内存中第一个字符的位置。
作为旁注,char name[10][n]; 是无效的;因为 n 必须是编译时常量。 我建议使用std::vector作为替代品。
如果您只想添加数字,则可以使用unordered_map。它类似于 java 中的哈希表。
- C++字符*缓冲区的大小
- HEX值到wchar_t字符(UTF-8)的转换
- 为什么 Serial.println(<char[]>);返回随机字符?
- 我的字符计数代码计算错误.为什么
- 字符串-C++后显示的随机字符
- 将Integer转换为4字节的unsined字符矢量(按大端字节顺序)
- 如何在C++中从字符串中分割字符
- 为什么msgrcv()将垃圾字符馈送到缓冲区
- 指向指向字符数组的指针数组的指针
- 如何用转义符替换字符串中的所有特殊字符
- 为什么 sscanf 无法从一个字符串中读取uint64_t和字符?
- 比较字符数组
- 将字符指针十六进制转换为字符串并保存在文本文件C++中
- 从矢量<无符号字符>转换为字符* 包括垃圾数据
- 如何使用Crypto++并为RSA返回可打印的字节/字符数组
- 如何在C++中确定文本文件中的元素是字符还是数字
- 如何将一个ostringstream十六进制字符串字符对转换为单个unit8t等价的二进制值
- 为字符串中每 N 个字符插入空格的函数没有按照我认为的方式工作?
- C++中无符号字符溢出
- 使用无符号字符数组有效存储内存