在以下代码示例中,将键设置为null和 System.gc()调用时,将WeakHashMap丢失所有映射并清空.
class WeakHashMapExample {
public static void main(String[] args) {
Key k1 = new Key("Hello");
Key k2 = new Key("World");
Key k3 = new Key("Java");
Key k4 = new Key("Programming");
Map<Key, String> wm = new WeakHashMap<Key, String>();
wm.put(k1, "Hello");
wm.put(k2, "World");
wm.put(k3, "Java");
wm.put(k4, "Programming");
k1=null;
k2=null;
k3=null;
k4=null;
System.gc();
System.out.println("Weak Hash Map :"+wm.toString());
}
}
class Key{
private String key;
public Key(String key) {
this.key=key;
}
@Override
public boolean equals(Object obj) {
return this.key.equals((String)obj);
} …Run Code Online (Sandbox Code Playgroud) 何时使用功能参考如
void (&fr)() = foo;
fr();
Run Code Online (Sandbox Code Playgroud)
而不是诸如的函数指针
void (*fp)() = &foo;
fp();
Run Code Online (Sandbox Code Playgroud)
有什么功能指针不能做但函数引用可以吗?
我有以下代码:
std::string getString() {
std::string str("hello");
return str;
}
int main() {
const char* cStr = getString().c_str();
std::cout << cStr << std::endl; // this prints garbage
}
Run Code Online (Sandbox Code Playgroud)
我以为会发生的是,getString()将返回一个复制的str(getString()按价值计算收益); 因此,副本str将保持"活着" main()直到main()返回.这将cStr指向一个有效的内存位置:返回的副本的底层char[]或char*(或其他)保留在哪里.strgetString()main()
但是,显然并非如此,因为程序输出垃圾.那么,问题是,何时被str销毁,为什么?
我有以下代码:
std::unordered_map<std::string, std::string> map;
map["k1"] = "v1";
auto& v1 = map["k1"];
map["k2"] = "v2";
Run Code Online (Sandbox Code Playgroud)
阅读http://en.cppreference.com/w/cpp/container/unordered_map后
笔记
交换函数不会使容器内的任何迭代器无效,但它们会使标记交换区域末尾的迭代器无效.
存储在容器中的键或数据的引用和指针只有通过擦除该元素才会失效,即使相应的迭代器无效也是如此.
v1即使在插入过程中可能发生重新散列,看起来在插入新值后也可以安全使用.
我对这句话的解释是否正确?我可以在修改地图后使用地图中值的引用/指针(显然擦除值本身会使引用/指针无效)?
我尝试使用new运算符来实例化特定的类,而不是new关键字后面的类.
我尝试为抽象类提供一种"工厂".
在我看来,这是不可能的,但让我们仔细检查!这段代码编译,但主代码将其视为Test(而不是TestImpl类)
class Test
{
public:
virtual int testCall() { return 0; };
static void* operator new(std::size_t);
};
class TestImpl : public Test
{
virtual int testCall() override
{
return i;
}
int i = 15;
};
void* Test::operator new(size_t sz)
{
return ::new TestImpl();
}
void main()
{
Test * t = new Test(); // Call the new operator, correctly
int i = test->testCall(); // i == 0 and not …Run Code Online (Sandbox Code Playgroud) 我想知道为什么
std::unique_ptr<MyClass> p = new MyClass;
Run Code Online (Sandbox Code Playgroud)
不起作用,但是
std::unique_ptr<MyClass> p;
p.reset(new MyClass);
Run Code Online (Sandbox Code Playgroud)
很好.我有点理解他们是如何不同的,但我想知道为什么做出选择使他们与众不同.分配与重置不一样的危险是什么?
I would expect that in C++20 the following code prints nothing between prints of A and B (since I expect guaranteed RVO to kick in). But output is:
A
Bye
B
C
Bye
Bye
So presumably one temporary is being created.
#include <iostream>
#include <tuple>
struct INeedElision{
int i;
~INeedElision(){
std::cout << "Bye\n";
}
};
std::tuple<int, INeedElision> f(){
int i = 47;
return {i, {47}};
}
INeedElision g(){
return {};
}
int main()
{
std::cout << "A\n";
auto x = …Run Code Online (Sandbox Code Playgroud) 我正在尝试制作模板包装函数,该函数应该转发参数并返回值。我无法决定使用什么auto&&或decltype(auto)返回类型更好。我读过 Scott Meyers 的文章,并理解与不剥离 ref_qualifiersdecltype(auto)相比,有必要返回。auto据我了解,同样的论点适用于使用auto&&over auto。现在我有以下问题:
decltype(auto)和何时没有区别?auto&&rvalue,例如:return int{};?返回值会是悬空引用吗?decltype(auto)和 和有什么区别auto&&?什么更适合作为远期回报类型?您好我想知道为什么C++标准允许我们在嵌套类中访问外部类的私有字段,而它禁止从外部类访问内部类的私有字段.我明白了,这个例子:
class OuterClass{
public:
class InnerClass{
public:
void printOuterClass(OuterClass& outer) {cout << outer.m_dataToDisplay;};
};
private:
int m_dataToDisplay;
};
Run Code Online (Sandbox Code Playgroud)
很好,因为内心,有时可能会很复杂.但我认为以下情况也很好:
class Algorithm{
public:
class AlgorithmResults{
public:
void readAlgorithmResult();
private:
void writeAlgorithmResult();
};
void calculate(AlgorithmResults& results, Arguments...){
//calculate stuff
results.writeAlgorithmResult(results);
}
};
Run Code Online (Sandbox Code Playgroud)
对我来说,这种结构非常有意义,尽管在C++中是不允许的.我也注意到,有一段时间两者都被Java所允许,但现在第二个例子也被禁止了.是什么原因,第一个例子被允许而另一个被拒绝?
我在模板化的lambda中遇到"if constexpr"的问题.为了论证,让我们忽略我是如何到达那里的,但我有一个结构foo,它以某种方式定义,产生如下内容:
template<bool condition>
struct foo {
int a;
// Only contains b if condition is true
int b;
}
Run Code Online (Sandbox Code Playgroud)
现在我可以定义一个模板函数thtemplate
template<bool condition>
void print_fun(foo & obj) {
/* Do something with obj.a */
if constexpr(condition)
/* Do something with obj.b */
};
Run Code Online (Sandbox Code Playgroud)
实例化这个功能,并使用它会编译,如果constexpr参数foo是一样的一个print_fun,即
constexpr bool no = false;
foo<no> obj = {};
print_fun<no>(obj);
Run Code Online (Sandbox Code Playgroud)
这会编译,因为假分支在模板化实体内被丢弃,因此在print_fun中使用obj.b没有问题.
但是,如果我定义一个类似的lambda表达式如下:
template<bool condition>
auto print_lambda = [](foo & obj) {
/* Do something with obj.a */
if constexpr(condition)
/* Do something …Run Code Online (Sandbox Code Playgroud) c++ ×9
c++11 ×3
c++14 ×1
c++17 ×1
c++20 ×1
copy-elision ×1
if-constexpr ×1
java ×1
lambda ×1
new-operator ×1
private ×1
return-type ×1
rvo ×1
stdtuple ×1
string ×1
templates ×1
unique-ptr ×1
weakhashmap ×1