简体   繁体   English

C ++ string()与c字符串的比较。 为什么要这样做?

[英]C++ string() comparison with a c-string. WHY DOES THIS WORK?

So this code is for a command input to be entered in any random order and it will return the value that comes after your input. 因此,此代码用于以任意随机顺序输入命令输入,它将返回输入后的值。 Amt_Range is a digit checking function. Amt_Range是一个数字检查功能。

Why does this work. 为什么这样做。 It should be able to due to the pointer comparison.?? 它应该能够由于指针的比较。

More importantly what does the string() do. 更重要的是string()的作用。 By my limited understanding the comparison should not work because ac style string is in the form '-' 't'. 据我有限的理解,由于ac样式字符串的格式为'-''t',因此比较不起作用。 Thanks in advance!! 提前致谢!!

int main(int argc, const char *argv[]) {

string dummy;
int tests = 0, quizzes = 0, assignments = 0, labs = 0, fin = 0;
int testweight = 0, quizweight = 0, assignweight = 0, labweight = 0, finweight = 0;
int counter = 1;

    if (argv[counter] == string("-t")) {
        dummy = argv[counter + 1];
        tests = Amt_Range(dummy, "tests");
        counter+=2;

    } else if (argv[counter] == string("-q")) {
        dummy = argv[counter + 1];
        quizzes = Amt_Range(dummy, "quizzes");
        counter+=2;


    } else if (argv[counter] == string("-a")) {
        dummy = argv[counter + 1];
        assignments = Amt_Range(dummy, "assignments");
        counter+=2;


    } else if (argv[counter] == string("-l")) {
        dummy = argv[counter + 1];
        labs = Amt_Range(dummy, "labs");
        counter+=2;


    } else if (argv[counter] == string("-f")) {
        dummy = argv[counter + 1];
        fin = Amt_Range(dummy, "whether there is a final");
        counter+=2;

    } else {
    cout << "wrong input NOW START OVER" << endl;
    exit(EXIT_FAILURE);

    }
}

The operator==() overload that kicks in is a free function in the std namespace. operator==()operator==()重载是std名称空间中的自由函数。

namespace std { 
     bool operator==(std::string const&, std::string const&);
}

It takes the first argument by const& which means that a temporary is welcome. 它采用const&的第一个参数,这意味着欢迎使用临时变量。

A temporary happens to be creatable using the implicit conversion constructor std::string(char const*) . 使用隐式转换构造函数std::string(char const*)可以创建一个临时对象。 So, the overload applies. 因此,过载适用。

UPDATE As uncovered in the comments, the standard actually declares 更新 正如注释中发现的那样,该标准实际上声明了

  bool operator==(const char*, std::string const&); 

as an optimization in §21.4.8.2 operator== . 作为§21.4.8.2operator ==中的优化。 However, the implicit conversion on the LHS is good to know about because it's a key ingredient in the language design with respect to operator overload (resolution) 但是,很高兴知道LHS上的隐式转换,因为它是语言设计中有关运算符重载(分辨率)的关键因素。


Now, the reason why the overload bool std::operator==(std::string const&, std::string const&) is even found during overload resolution is slightly subtle. 现在,在重载解析期间甚至发现重载bool std::operator==(std::string const&, std::string const&)原因有些微妙。 This mechanism is known as Argument Dependent Lookup. 此机制称为“参数依赖查找”。

In this case, ADL works because the second argument is already a std::string , which has a type declared in the std namespace. 在这种情况下,ADL之所以有效,是因为第二个参数已经是std::string ,它具有在std命名空间中声明的类型。 Therefore, this namespace is searched for candidate operator== overloads 因此,在此命名空间中搜索候选operator==重载


DISCLAIMER: 免责声明:

I've simplified the above. 我已经简化了上面的内容。 In reality the code in the standard library is much more generic still, and probably looks more like 实际上,标准库中的代码仍然更加通用,并且看起来可能更像

namespace std { 
     template <typename Char, typename CharTraits, typename Alloc>
     bool operator==(std::basic_string<Char, CharTraits, Alloc> const&, std::basic_string<Char, CharTraits, Alloc>  const&) {
          // implementation...
     }
}

Quote from the standard (C++14): 引用标准(C ++ 14):

21.3 String classes [string.classes] 21.3字符串类[string.classes]

 template<class charT, class traits, class Allocator> bool operator==(const basic_string<charT,traits,Allocator>& lhs, const basic_string<charT,traits,Allocator>& rhs) noexcept; template<class charT, class traits, class Allocator> bool operator==(const charT* lhs, const basic_string<charT,traits,Allocator>& rhs); template<class charT, class traits, class Allocator> bool operator==(const basic_string<charT,traits,Allocator>& lhs, const charT* rhs); 

[... same for != < > <= >= ] [...为相同!= < > <= >= ]

Or look in this section: 或查看此部分:

21.4.8.2 operator== [string::operator==] 21.4.8.2 operator== [string::operator==]

So there is an exact match, which is found in namespace std (would use ADL (argument-dependent lookup, counting the arguments namespaces as associated namespaces and searching them too) if there was no using-directive in scope). 因此,存在一个完全匹配,如果在范围内没有using指令,则在名称空间std找到(将使用ADL(依赖于参数的查找,将参数名称空间视为关联的名称空间并进行搜索))。

BTW: I'm a bit disappointed that two std::basic_string s differing only in allocator-type cannot be compared (template-arguments must match exactly)... 顺便说一句:我有点失望的是,无法比较两个std::basic_string仅在分配器类型上不同(模板参数必须完全匹配)...
A quick demo on coliru: http://coliru.stacked-crooked.com/a/01788a718178c6d2 关于大肠杆菌的快速演示: http ://coliru.stacked-crooked.com/a/01788a718178c6d2

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

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