小编Lud*_*ert的帖子

相同的typeid名称,但不是std :: is_same

使用C++(GCC 4.8.3)我有2种类型(T1T2),其具有奇怪的属性,typeid(T1).name()并且typeid(T2).name()是相同的,但 std::is_same<T1, T2>::valuefalse.

怎么可能?我如何进一步调查,告诉原因可能是什么?

c++ c++11

18
推荐指数
2
解决办法
1574
查看次数

STRING_AGG 带换行符

DROP TABLE IF EXISTS items;
CREATE TABLE items (item varchar(20));
INSERT INTO items VALUES ('apple'),('raspberry');
SELECT STRING_AGG(item, CHAR(13)) AS item_list FROM items;
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

如何在项目之间换行?

sql t-sql sql-server string-aggregation

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

可以避免范围最小重新计算吗

目标是在一定范围的输入值上最小化函数。性能很重要。不幸的是,该ranges::min()算法一遍又一遍地重新计算实时最优的输出。

看起来该算法可以缓存与最佳值相对应的输出值,或者我错过了什么?

在这个例子中,为什么f(x=0)需要调用n次呢?

#include <ranges>
#include <algorithm>
#include <stdio.h>
using namespace std;

int main()
{
    auto f=[](int x){
        printf("calling f(x=%d)\n", x);
        return x*x;
    };
    auto rg = views::iota(0,4);
    int x1 = ranges::min(rg, {}, f);
}
Run Code Online (Sandbox Code Playgroud)

它输出:

调用 f(x=0)
调用 f(x=1)
调用 f(x=0)
调用 f(x=2)
调用 f(x=0)
调用 f(x=3)

有没有ranges::min()更优化的调用方式?

c++ min minimization c++20 std-ranges

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

范围视图大小无法编译

#include <iostream>
#include <cstdlib>
#include <vector>
#include <ranges>
#include <algorithm>
using namespace std;


int main()
{
    vector<int> ints = {1,2,3,4,5};
    auto v = ints | views::take_while([](int i){return i<3;}) ;
    for (int i : v) std::cout << i << ' ';
    std::cout << '\n';
    int size = v.size();
    std::cout << size << std::endl;
}
Run Code Online (Sandbox Code Playgroud)

v.size() 无法编译。你怎么做到这一点 ?

prog.cc: In function 'int main()':
prog.cc:16:23: error: no matching function for call to 'std::ranges::take_while_view<std::ranges::ref_view<std::vector<int> >, main()::<lambda(int)> >::size()'
   16 |     int size = v.size();
      | …
Run Code Online (Sandbox Code Playgroud)

c++ size c++20 std-ranges

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

C++有扩展运算符吗?

你能在 C++ 中做到这一点吗:

vector<int> v1={1,2}, v2={3,4};
vector<int> v3={...v1, ...v2, 5};
//v3 = {1,2,3,4,5}
Run Code Online (Sandbox Code Playgroud)

使用 C++ 执行此操作的最简单方法是什么?

c++ arrays spread-syntax

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

range::sort 无法编译

在 MyRect.h 中:

\n
struct MyRect\n{\n    MyRect(std::initializer_list<int> i);\n    MyRect();\n    int16_t m_left=0, m_right=0, m_top=0, m_bottom=0 ;\n    int16_t no_sequence=0 ;\n    int16_t i=-1 ;\n    bool selected=false ;\n} ;\n\nbool operator==(const MyRect& r1, const MyRect& r2) ;\nbool operator<(const MyRect& r1, const MyRect& r2);\n
Run Code Online (Sandbox Code Playgroud)\n

在 MyRect.cpp 中:

\n
bool operator==(const MyRect& r1, const MyRect& r2)\n{\n    return r1.m_left==r2.m_left &&\n        r1.m_right==r2.m_right &&\n        r1.m_top==r2.m_top &&\n        r1.m_bottom==r2.m_bottom ;\n}\nbool operator<(const MyRect& r1, const MyRect& r2)\n{\n    if (r1.m_left != r2.m_left)\n        return r1.m_left < r2.m_left;\n    if (r1.m_right != r2.m_right)\n        return r1.m_right < …
Run Code Online (Sandbox Code Playgroud)

c++ operator-overloading c++20 std-ranges

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

SQL Server子字符串无上限

SQL Server中最好的方法是什么

SELECT 
    SUBSTRING('CATCH ME IF YOU CAN', 2, 10000)
    -- ATCH ME IF YOU CAN
Run Code Online (Sandbox Code Playgroud)

没有上限?

sql sql-server

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

使用多字符分隔符的 C++ 视图 join_with

使用 C++23 视图::join_with 时可以使用多字符串作为分隔符吗?

gcc 14 返回错误消息。

#include <format>
#include <stdio.h>
#include <string>
#include <algorithm>
#include <ranges>
using namespace std;

int main(){
    const string a[2]={"6", "7"};
    const string buffer1 = a | views::join_with('\n') | ranges::to<string>(); //OK
    printf("%s", buffer1.c_str());

    const string buffer2 = a | views::join_with(",\n") | ranges::to<string>(); //KO
    printf("%s", buffer2.c_str());
    return 0;
}
Run Code Online (Sandbox Code Playgroud)
prog.cc: In function 'int main()':
prog.cc:13:30: error: no match for 'operator|' (operand types are 'const std::string [2]' {aka 'const std::__cxx11::basic_string<char> [2]'} and 'std::ranges::views::__adaptor::_Partial<std::ranges::views::_JoinWith, const char*>')
   13 | …
Run Code Online (Sandbox Code Playgroud)

c++ std-ranges c++23

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