VS代码
if (1 == 1) // When I hit enter here, the cursor moves to the next line at pos (0)
|
else
return; // When I hit enter here, cursor moves to next line/ pos 4
| // why is the cursor here? why not pos 0?
Run Code Online (Sandbox Code Playgroud)
Visual Studio(这就是我想要发生的事情)
if (1 == 1) // When I hit enter here, the cursor moves to the next line at
| // pos 1 if I'm working with Tabs or pos 4 …Run Code Online (Sandbox Code Playgroud) 简单的方法是显而易见的
std::map<int,std::unique_ptr<something>> mymap;
auto f = mymap.find(5);
std::unique_ptr<something> myptr;
if (f == mymap.end())
mymap.insert({5, std::move(myptr)});
Run Code Online (Sandbox Code Playgroud)
但是,这看起来效率不高,因为我必须在地图中找到两次键.一个用于检查密钥是否不存在,并且插入功能也将执行相同的操作.
如果我只是使用mymap.insert({5, std::move(myptr)});那么我的唯一ptr(myptr)如果pair.second返回false(键已经存在)就消失了.
编辑:
显然答案是在C++ 17上try_emplace,并且它已经在我正在使用的编译器中可用(vs2015),而且由于我正在处理个人项目,我可以负担得起它.
struct base
{
base() { throw std::exception(); }
};
struct derived : public base
{
derived() try : base() { }
catch (std::exception& e)
{
std::cout << "exception handled" << std::endl;
}
};
int main()
{
derived a; // My app crashes.
return 0;
}
Run Code Online (Sandbox Code Playgroud)
不是我的应用程序写"异常处理"并继续运行?
我发现的唯一解决方案是a在try/catch块中包围" " 的构造.但是,如果我这样做,那么首先在构造函数中使用try/catch有什么意义呢?我猜也许它的用途是清理可能已分配的成员变量?既然没有调用析构函数?
以下工作,但处理异常2次.
struct base
{
base() { throw std::exception(); }
};
struct derived : public base
{
derived() try : base() { }
catch(std::exception& e)
{
std::cout …Run Code Online (Sandbox Code Playgroud) struct A
{
int a = 1;
short b = 2;
char c = 3;
}
struct B
{
using arr_type = array<A,3>;
char asd = 0;
A a1;
A a2;
A a3;
// is this safe to use to loop trough all 3 elements?
arr_type* p1 = reinterpret_cast<arr_type*>(&a1);
// or maybe this one?
A* p2 = &a1;
};
Run Code Online (Sandbox Code Playgroud)
我可以安全地使用p1或p2循环播放a1...a3吗?
B b;
for (int i = 0; i < 3; i++)
{
cout << …Run Code Online (Sandbox Code Playgroud) 根据我的理解,以下代码效率不高:
class Foo {
static Resource resource1;
static Resource resource2;
static synchronized void methodA() {
resource1.add("abc");
}
static synchronized void methodB() {
resource2.add("abc");
}
}
Run Code Online (Sandbox Code Playgroud)
根据我的理解,两种方法都锁定在一个对象(类对象Foo.class)中,所以我猜测以下是一个很好的优化?
class Foo {
static Resource resource1;
static Resource resource2;
static void methodA() {
synchronized(resource1) {
resource1.add("abc");
}
}
static void methodB() {
synchronized(resource2) {
resource2.add("123");
}
}
}
Run Code Online (Sandbox Code Playgroud)
只要这两种资源不相互依赖.
我什么时候应该考虑使用静态同步方法?
struct Something
{
int a;
int b;
Something(char* buffer)
{
memcpy(this, buffer, sizeof(Something));
};
};
Run Code Online (Sandbox Code Playgroud)
这合法吗?安全?对我来说它看起来很好,但我不确定C++标准是否以某种方式禁止它.
使用“ -Xclang -std=c++17”我可以构建可执行文件,但是我找不到激活 c++17 智能感知的内容。我已经尝试了很多组合,如下所示,但似乎都不起作用
CMakeLists.txt
cmake_minimum_required(VERSION 3.9.2)
set(CMAKE_CXX_STANDARD 17)
project(myapp)
add_compile_options("-Xclang" "-std=c++17")
add_executable(myapp main.cpp)
set_target_properties(myapp PROPERTIES CXX_STANDARD 17)
target_compile_features(myapp PRIVATE cxx_std_17)
Run Code Online (Sandbox Code Playgroud)
主程序
#include <tuple>
namespace test1::test2 // red [qualified name is not allowed]
// ^^^^^^^^^^^^^
{}
int main()
{
auto[a, b] = std::pair<int, int>();
// ^^^^^^
return 0;
}
Run Code Online (Sandbox Code Playgroud)
CMakeSettings.json
{
// See https://go.microsoft.com//fwlink//?linkid=834763 for more information about this file.
"configurations": [
{
"name": "x64-Debug",
"generator": "Ninja",
"configurationType": "Debug",
"inheritEnvironments": [ "msvc_x64_x64" ],
"buildRoot": "${env.USERPROFILE}\\CMakeBuilds\\${workspaceHash}\\build\\${name}",
"installRoot": "${env.USERPROFILE}\\CMakeBuilds\\${workspaceHash}\\install\\${name}",
"cmakeCommandArgs": …Run Code Online (Sandbox Code Playgroud) void HelloWorld()
{
static std::atomic<short> static_counter = 0;
short val = ++static_counter; // or val = static_counter++;
}
Run Code Online (Sandbox Code Playgroud)
如果从两个线程调用此函数,
局部变量val可以1在两个线程中吗?或(如果static_counter++使用,则为0 ?)
我对android很新.希望这不是一个愚蠢的问题.
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new Thread(new Runnable()
{
public void run()
{
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
startActivity(intent);
}
}).start();
}
Run Code Online (Sandbox Code Playgroud)
问题:为什么这有效?从辅助线程调用startActivity是正常的吗?
我认为所有与UI相关的事情都必须在UI线程中完成.
假设我想拥有一个价格不同的苹果容器.我希望它们按价格排序(最高价格优先),但我也希望通过他们的ID快速检索它们.到目前为止我所拥有的是以下内容
struct AppleClass
{
string id;
int price;
bool operator<(const AppleClass& o) const
{
return price > o.price;
}
};
int main()
{
set<AppleClass> myapples;
myapples.insert({"apple1", 500});
myapples.insert({"apple2", 600});
myapples.insert({"apple3", 400});
for (auto& apple : myapples)
{
cout << apple.id << "," << apple.price << endl;
}
}
Run Code Online (Sandbox Code Playgroud)
我的应用程序将花费20%的时间删除条目,20%插入条目,25%检索它们(检索整个列表),35%更新它们(它们的价格将增加或减少)经常.
容器最多可包含450个条目.
我的代码只解决了排序问题.查找是无用的,因为我想通过他们的id找到(所以我需要迭代所有这些).出于同样的原因,删除和插入会很慢.
这感觉是错误的选择.
但是,如果我有一张地图,那么它将根据id进行排序.每次我检索列表时,我都必须将其复制到某个容器中,例如,订购它,然后将其发送给用户,这也感觉很慢.
救命!
// bind function:
template<typename T> T bind(T& v)
{
// Can I toy with the object v refers to? or that's undefined behaviour?
// the object v refers to is not initialized yet, But the object has been allocated, so I can use that memory, I think?
// The following 2 lines should be fine if I'm correct.
// Which is my function is currently is doing (sorta)
myvector.emplace_back(SQLType<T>(), (void*)&v);
return 0;
}
SomeClass value = bind(value);
Run Code Online (Sandbox Code Playgroud)
我想知道我是否可以value在初始化之前使用该对象(当 …