小编Lea*_*pcs的帖子

如何比较 const char* 字符串?

首先,考虑这个例子:

#include <iostream>
using namespace std;

int main()
{
    cout << ("123" == "123");
}
Run Code Online (Sandbox Code Playgroud)

我期望什么:由于“123”是一个const char*,我期望对这些字符串的地址(如这些答案之一所述)进行比较。

...因为!===只会比较这些字符串的基地址。不是字符串本身的内容。

但输出仍然是1. 好吧,我们实际上不知道如何比较两个纯右值对象的地址(或者至少我不明白它是如何完成的)。因此,让我们将这些字符串声明为变量,看看会发生什么:

#include <iostream>
using namespace std;

int main()
{
    const char* a = "1230";
    const char* b = "1230";
    cout << (a == b);
}
Run Code Online (Sandbox Code Playgroud)

输出仍然是1. 那么const char*琴弦不会衰减吗?或者编译器设法进行一些优化并仅为一个字符串分配内存?好吧,让我们尝试避免它们:

#include <iostream>
using namespace std;

int main()
{
    const char* a = "1230";
    const char* b = "1231";
    b …
Run Code Online (Sandbox Code Playgroud)

c++ pointers string-literals relational-operators

3
推荐指数
1
解决办法
453
查看次数

以 sizeof ... (args) == 0 作为基本情况的参数包的函数无法编译

这是我的函数的代码:

#include <iostream>
#include <type_traits>
#include <algorithm>

template <typename Head, typename ... Args>
std::common_type_t<Head, Args...> mx(Head n, Args ... args)
{
    if (sizeof ... (args) == 0)
        return n;
    else
        return std::max(n, mx(args ...));
}

int main()
{
    std::cout << mx(3, 4, 5);
}
Run Code Online (Sandbox Code Playgroud)

我遇到编译错误:

main.cpp:在 'std::common_type_t<Head, Args ...> mx(Head, Args ...) 的实例化中 [with Head = int; 参数 = {}; std::common_type_t<Head, Args ...> = int]': main.cpp:11:24:
从 'std::common_type_t<Head, Args ...> mx(Head, Args ...) 递归地需要[头= int; 参数 = {int}; std::common_type_t<Head, Args …

c++ variadic-functions variadic-templates

2
推荐指数
1
解决办法
254
查看次数

我可以以某种方式优雅地禁止在模板函数中使用未签名的变量吗?

考虑这段代码:

template <typename T>
T abs(const T& n)
{
    if (!std::is_signed<T>::value)
        throw logic_error;
    if (n < 0)
        return -n;
    return n;
}
Run Code Online (Sandbox Code Playgroud)

我想完全禁止我的函数与变量的使用unsigned,因为它没有意义,而且可能用户甚至不知道他使用了变量unsigned。例如,我可以在某种程度上避免这个问题:

template <typename T>
T abs(const T& n)
{
    if constexpr(!std::is_signed<T>::value)
        n += "";
    if (n < 0)
        return -n;
    return n;
} 
Run Code Online (Sandbox Code Playgroud)

但如果我调用abs(4u),编译器错误不是很明显。就像是"can't apply += const char[1] to double"。我可以让它更明显吗?或者只是进行多次重载?

c++ templates overloading

1
推荐指数
1
解决办法
134
查看次数

具有重载 less 运算符的 std::max 无法编译

这是代码:

#include <iostream>
#include <fstream>
#include <string>
#include <algorithm>
#include <utility>

struct student
{
    std::string full_name;
    int group;
    int number;

    friend std::istream& operator>>(std::istream& in, student& obj)
    {
        in >> obj.full_name >> obj.group >> obj.number;
        return in;
    }
    friend std::ostream& operator<<(std::ostream& out, const student& obj)
    {
        out << "Full name: " << obj.full_name << '\n';
        out << "Group: " << obj.group << '\n';
        out << "Number: " << obj.number << '\n';
        return out;
    }

    bool operator<(const student& obj)
    {
        return this->number …
Run Code Online (Sandbox Code Playgroud)

c++ overloading operator-overloading std max

0
推荐指数
1
解决办法
206
查看次数