简体   繁体   English

如何在C ++中比较两个字符串并使用默认函数?

[英]How to compare two strings in c++ and work with default functions?

I am working on my first ever c++ code and i am having some diffuclties the strcmp function couldn't be resolved!! 我正在编写我的第一个C ++代码,但遇到一些困难,无法解决strcmp函数! Even though i did in h file: using std::string; 即使我在h文件中using std::string; I am working with Eclipse, so I did an h and cpp files in the h file i have this : 我正在使用Eclipse,所以我在h文件中做了一个h和cpp文件,我有这个:

#include <iostream>
#include <string>
#include <set>

using std::set;
using std::string;

namespace AA{
namespace BB{


    typedef enum{
        EASY , MEDIUM, HARD
    } Difficulty;

    class FIRST{
    private:
        char* name;
        Difficulty difficulty;

    public:       
        FIRST(const std::string& name, const Difficulty& difficulty);

        FIRST(const FIRST& first) = default;




    };
} // end of namespace BB
} 

// end of namespace AA

and in the cpp file I did this but i get eror that 在cpp文件中,我做到了,但是我得到了错误

Function strcmp could not be resolved 'strcmp' is not a member of 'std' 无法解析函数strcmp '不是'std'的成员

#include <iostream>
#include <string>
#include <set>
#include "Enigma.h"
#include <stdbool.h>

AA::BB::FIRST(const std::string& name, const Difficulty& difficulty):
    difficulty(difficulty),  name(name) {
}


bool AA::BB::FIRST::operator==(const FIRST& first) const{
    if((difficulty==(FIRST.difficulty))&&(strcmp(name,first.name)==0)){
        return true;
    }
    return false;
}

Why compare with strcmp , when you can make use of the existing == operator: 当可以使用现有的==运算符时,为什么要与strcmp进行比较:

bool AA::BB::FIRST::operator==(const FIRST& first) const{
    if((difficulty == FIRST.difficulty) && (name == first.name)){
        return true;
    }
    return false;
}

Or you could use its compare function which would yield the same result: 或者,您可以使用其比较功能来产生相同的结果:

bool AA::BB::FIRST::operator==(const FIRST& first) const{
    if((difficulty == FIRST.difficulty) && (name.compare(first.name) == 0){
        return true;
    }
    return false;
}

When you want to use a certain function you can go to some reference site , find out which header should be included in order to use this function ( <cstring> in this case) and then include it. 当您想使用某个功能时,可以转到某个参考站点 ,找出应该包含哪个标头才能使用此功能(在本例中为<cstring> ),然后将其包含在内。

You should also figure out the code style you are using. 您还应该弄清楚正在使用的代码样式。 CAPS names are typically only used for macros (because they are evil). CAPS名称通常仅用于宏(因为它们是邪恶的)。

The function strcmp is C. There are no strings as you know them from C++. 函数strcmp是C。C++中没有字符串。 See here: http://www.cplusplus.com/reference/cstring/strcmp/ 看到这里: http : //www.cplusplus.com/reference/cstring/strcmp/

You could do: 您可以这样做:

#include <string>

int main()
{
    std::string name = "Webber";
    std::string firstName = "John";

    if (strcmp(name.c_str(), firstName.c_str()) == 0)
        ...
}

This will copy your C++ String to an char-Array 这会将您的C ++字符串复制到char-Array

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM