在一个采用相同类型的多个参数的函数中,我们如何保证调用者不会搞乱排序?
例如
void allocate_things(int num_buffers, int pages_per_buffer, int default_value ...
Run Code Online (Sandbox Code Playgroud)
然后
// uhmm.. lets see which was which uhh..
allocate_things(40,22,80,...
Run Code Online (Sandbox Code Playgroud) 我正在尝试在bash中创建一个备份脚本,以tar文件夹的内容并将生成的文件移动到某个地方,但我真的不知道该怎么做.
#!/bin/bash
name="$date +"%y-%m-%d""
tar -zcvf $name code
Run Code Online (Sandbox Code Playgroud)
但结果是该文件刚刚命名+%y-%m-%d.如何更改脚本以按预期的日期命名文件?
预期产量: 2013-08-29.tar
我有一种数据类型必须存储在一个连续的数组中,为了更新这些数据,迭代的数组被迭代.棘手的部分是我希望有可能动态地改变任何对象的更新方式.
这是我到目前为止所提出的:
struct Update {
virtual void operator()(Data & data) {}
};
struct Data {
int a, b, c;
Update * update;
};
struct SpecialBehavior : public Update {
void operator()(Data & data) override { ... }
};
Run Code Online (Sandbox Code Playgroud)
然后我会为每个数据对象分配一些类型的Update.然后在更新期间,所有数据都会传递给自己的更新仿函数:
for (Data & data : all)
data->update(data);
Run Code Online (Sandbox Code Playgroud)
据我所知,这就是战略模式.
我的问题:有没有办法更有效地做到这一点?某些方法可以实现相同的灵活性而无需调用虚拟方法的成本?
试图创建一个唯一的id生成函数,并想出了这个:
std::atomic<int> id{0};
int create_id() {
id++;
return id.load();
}
Run Code Online (Sandbox Code Playgroud)
但我认为该函数可能两次返回相同的值,对吧?例如,线程A调用该函数,递增该值,但随后在线程B进入时停止并且还递增该值,最后A和B都返回相同的值.
所以使用互斥锁,函数可能如下所示:
std::mutex mx;
int id = 0;
int create_id() {
std::lock_guard<std::mutex> lock{mx};
return id++;
}
Run Code Online (Sandbox Code Playgroud)
我的问题:是否有可能仅使用原子创建从计数器生成唯一int值的行为?我问的原因是因为我需要产生很多id,但是读到mutex很慢.
在这种情况下:
struct Holder {
std::function<void()> f;
};
struct Functor { void operator()(){ /**/ } };
int main() {
Holder = { Functor{} };
//...
Run Code Online (Sandbox Code Playgroud)
有没有办法后来f回归到一个Functor类型?
此代码有效:
struct Blob {
static constexpr int a = 10;
};
int main() {
Blob b;
auto c = b.a;
}
Run Code Online (Sandbox Code Playgroud)
但是,如果我改为int,float我会收到一个错误:
struct Blob {
static constexpr float a = 10.0f;
};
Run Code Online (Sandbox Code Playgroud)
/tmp/main-272d80.o:在函数
main': main.cpp:(.text+0xe): undefined reference toBlob :: a'中
为什么我不能constexpr float以这种方式使用?
编译器:Ubuntu clang版本3.5.0-4ubuntu2(标签/ RELEASE_350/final)
在gcc版本4.9.1(Ubuntu 4.9.1-16ubuntu6)上测试并且没有错误.
编辑:
如果我使用-O1,-O2,-O3或-Os但它会在-O0失败时进行编译
鉴于这种类型:
std::map<int, std::vector<T>> map;
Run Code Online (Sandbox Code Playgroud)
如何直接放置元素?
map.emplace(10, /* ? */);
Run Code Online (Sandbox Code Playgroud)
我可以通过首先单独创建向量,然后在对 emplace 的调用中使用该标识符来做到这一点,但这是不可取的,以防可以以某种方式直接进行。
如何从两个元组列表中添加元组以获取结果的新列表?
例如:
a = [(1,1),(2,2),(3,3)]
b = [(1,1),(2,2),(3,3)]
Run Code Online (Sandbox Code Playgroud)
我们想得到
c = [(2,2),(4,4),(6,6)]
Run Code Online (Sandbox Code Playgroud)
我搜索谷歌并发现许多结果如何使用zip简单地添加两个列表,但找不到有关两个元组列表的任何内容.
所以我得到了非常好的第6版,我正在尝试cmake并制作示例代码框架,但是得到了这个错误:
Scanning dependencies of target sb6
[ 1%] Building CXX object CMakeFiles/sb6.dir/src/sb6/sb6.cpp.o
[ 2%] Building CXX object CMakeFiles/sb6.dir/src/sb6/sb6ktx.cpp.o
[ 3%] Building CXX object CMakeFiles/sb6.dir/src/sb6/sb6object.cpp.o
[ 5%] Building CXX object CMakeFiles/sb6.dir/src/sb6/sb6shader.cpp.o
[ 6%] Building C object CMakeFiles/sb6.dir/src/sb6/gl3w.c.o
Linking CXX static library lib/libsb6.a
[ 6%] Built target sb6
Scanning dependencies of target alienrain
[ 7%] Building CXX object CMakeFiles/alienrain.dir/src/alienrain/alienrain.cpp.o
Linking CXX executable bin/alienrain
CMakeFiles/alienrain.dir/src/alienrain/alienrain.cpp.o: In function `sb6::application::run(sb6::application*)':
alienrain.cpp:(.text._ZN3sb611application3runEPS0_[_ZN3sb611application3runEPS0_]+0x21): undefined reference to `glfwInit'
alienrain.cpp:(.text._ZN3sb611application3runEPS0_[_ZN3sb611application3runEPS0_]+0x7a): undefined reference to `glfwOpenWindowHint'
alienrain.cpp:(.text._ZN3sb611application3runEPS0_[_ZN3sb611application3runEPS0_]+0x90): undefined reference to `glfwOpenWindowHint' …Run Code Online (Sandbox Code Playgroud) 当我在某个时刻添加一个cout时,我很惊讶地看到我的程序突然变得安静,所以我隔离了负责的代码:
std::stringstream data;
data<<"Hello World\n";
std:std::fstream file{"hello.txt", std::fstream::out};
file<<data.rdbuf();
std::cout<<"now rdbuf..."<<std::endl;
std::cout<<data.rdbuf()<<std::endl;
std::cout<<"rdbuf done."<< std::endl;
Run Code Online (Sandbox Code Playgroud)
该计划在没有最后一场比赛的情况下悄然退出.到底是怎么回事?如果我改变了最后一个.rdbuf(),.str()那么它就完成了.