比较 c++ 中的字符

Comparing char in c++

本文关键字:字符 c++ 比较      更新时间:2023-10-16

这是我从中获取数据的文本文件

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 中的哈希表。