其声明如下:
template<class T,class Compare=less<T>,class Alloc=allocator<T>> class set;
众所周知,"集合"中没有两个相等的键.它使用"比较"(默认为less<T>)来比较两个键,这意味着它只能知道一个键比另一个键少(less<T>返回true)或不少(less<T>返回false).
如何set避免存在两个相等的键?
我试图覆盖C ++中的虚函数。在我重写该函数之后,它实际上并未覆盖它,因此使该类成为抽象的。下面的代码将使您对问题有很好的了解。
正如您在下面看到的,该代码对于像int这样的非指针模板也能正常工作,但使用int指针失败。
我以为也许是因为引用指针存在问题,所以我在实现Derived2的过程中取出了&,但并没有解决。
template<class T>
class Base {
virtual void doSomething(const T& t) = 0;
};
class Derived1: public Base<int>{
void doSomething(const int& t) {
} // works perfectly
};
class Derived2: public Base<int*>{
void doSomething(const int*& t) {
}
// apparently parent class function doSomething is still unimplemented, making Derived2 abstract???
};
int main(){
Derived1 d1;
Derived2 d2; // does not compile, "variable type 'Derived2' is an abstract class"
}
Run Code Online (Sandbox Code Playgroud) 为什么bitor与cout运算符一起使用时不起作用
这有效
int a=5,b = 6,d = a bitor b;
cout << d << endl;
Run Code Online (Sandbox Code Playgroud)
这引发错误
int a=5,b=6;
cout << a bitor b << endl;
Run Code Online (Sandbox Code Playgroud)
错误信息:
invalid operands of types 'int' and '<unresolved overloaded function type>' to binary 'operator<<'
cout << a bitor b << endl;
Run Code Online (Sandbox Code Playgroud) 我试图了解移动构造函数和右值引用。所以我在https://www.onlinegdb.com/online_c++_compiler上尝试了此代码。但是结果使我感到困惑。
#include <iostream>
#include <type_traits>
class A {
public:
A() { std::cout << "Constructed" << std::endl; }
A(const A& )= delete;
A(A&&) { std::cout << "Move Constructed" << std::endl; }
};
int
main ()
{
A&& a = A();
A b = a; // error: use of deleted function ‘A::A(const A&)’
//A b = static_cast<decltype(a)>(a); // This works, WTF?
std::cout << std::is_rvalue_reference<decltype(a)>::value << std::endl; // pretty sure a is rvalue reference.
return 0;
}
Run Code Online (Sandbox Code Playgroud) 我有以下 C++ 代码。
string getName()
{
return "C++";
}
void printName(const char* name)
{
cout << name << endl;
}
int main()
{
printName(getName().c_str());
}
Run Code Online (Sandbox Code Playgroud)
该函数getName返回一个string. 我将函数c_str的指针传递给函数。我想知道在调用函数之前返回的内容是否会被删除。如果不是那么什么时候删除返回值。stringprintNamestringprintName()
我需要知道以下内容通常是否有效。
string s = "some value";
string v = s.substr(0, 50).c_str();
Run Code Online (Sandbox Code Playgroud)
分配v始终有效吗?由于所传回的物件的生存期会不会出现任何问题substr()?
如何在 C++ 中声明一个二维指针数组并用空指针初始化它?
我试图这样做
int *arr[20][30]= nullptr;
Run Code Online (Sandbox Code Playgroud) 我找到了一个 C++ 代码,我们将初始化为最大值的 unsigned int 32 转换为有符号 int 并期望它是-1. 它在经过测试的编译器上运行良好,但它真的可移植吗?
int GetBusinessDataID()
{
u32_t id = ~0;
// Some code that may return a valid ID.
return id; // Here we expect to return -1.
}
Run Code Online (Sandbox Code Playgroud) 我试图从另一个类中声明的数组中获取一些值。该数组具有固定长度和常量元素(我将 100% 从不修改其值,所以这就是我将其设为常量的原因)。
但是,当我尝试访问main函数中的第一个元素时,出现编译错误:
basavyr@Roberts-MacBook-Pro src % g++ -std=c++11 main.cc
Undefined symbols for architecture x86_64:
"Vectors::vec1", referenced from:
_main in main-c29f22.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1
Run Code Online (Sandbox Code Playgroud)
如您所见,我正在使用 clang(最新版本)在 macOS Catalina 上进行编译。
[问]:可能是什么问题?先感谢您。
这是代码:
#include <iostream>
class Dimension
{
public:
static constexpr int dim1 = 2;
static constexpr int dim2 = 1;
};
class Vectors
{
public:
static constexpr double vec1[2] = {4.20, 6.9};
};
int main()
{ …Run Code Online (Sandbox Code Playgroud) 如果我有代码:
struct Test
{
int x = 10;
};
int main()
{
std::list<Test> linkedList;
std::cout << linkedList.front().x << std::endl;
}
---
out -> 0
Run Code Online (Sandbox Code Playgroud)
为什么我的 test.x 值是 0?如果我将列表更改为 int 类型,它返回 0。如果我给它一个 char 类型,我什么也得不到(或“”)。
我很好奇它是如何(以及为什么)发生在引擎盖下的。它如何处理返回任何类型的值而不退出程序或需要尝试/捕获?
c++ ×10
c++11 ×3
lifetime ×2
string ×2
temporary ×2
arrays ×1
class ×1
constexpr ×1
inheritance ×1
null ×1
overriding ×1
pointers ×1
polymorphism ×1
return-value ×1
set ×1
stdlist ×1
stl ×1
templates ×1
unsigned ×1