假设我的项目CMakeLists.txt包括foo.cmake:
include(foo)
Run Code Online (Sandbox Code Playgroud)
在foo.cmake,我想知道的道路foo.cmake.
我怎样才能做到这一点?
请注意,CMAKE_CURRENT_LIST_DIR给出包含的目录CMakeLists.txt,而不是包含的目录,foo.cmake因此不是我想要的.
当然,foo.cmake可能包括几个项目(即几个CMakeLists.txt文件).
假设我有一个std::vector整数:
std::vector<int> v;
Run Code Online (Sandbox Code Playgroud)
v包含 100 个元素,我想删除最后 10 个元素。我可以想到这个解决方案:
v.erase(v.end() - 10, v.end());
Run Code Online (Sandbox Code Playgroud)
有什么更好的吗?
我认为这对于了解模板的人来说是微不足道的......
假设我们想要这个模板类的两个不同的实现,具体取决于N的值:
template <int N>
class Foo {
...
};
Run Code Online (Sandbox Code Playgroud)
例如:
template <int N>
class Foo {
... // implementation for N <= 10
};
template <int N>
class Foo {
... // implementation for N > 10
};
Run Code Online (Sandbox Code Playgroud)
我们怎样才能在C++ 11中做到这一点?
如何设置 Jenkins 为特定工作保留的构建数量?
Jenkins 保留了我们所有工作的最后 30 个版本。但是,对于特定的工作,我们希望保留 60 个构建版本,而不是 30 个。
我转到作业的配置页面,勾选“放弃旧版本”,在“要保留的最大构建数量”中输入“60”,保存作业配置,然后重新启动 Jenkins。
在此更改之后,Jenkins 仍然只保留了 30 个作业构建。我究竟做错了什么?
相关问题:为什么只保留 30 个构建?(我认为默认是不删除任何构建。)
我们在 Linux 下使用 Jenkins 版本 2.164.1。
我正在使用 C++17。
std::set是一个模板类型:
template<
class Key,
class Compare = std::less<Key>,
class Allocator = std::allocator<Key>
> class set;
Run Code Online (Sandbox Code Playgroud)
可以将 astd::set作为数据成员。例如:
#include <set>
class Foo
{
std::set<size_t> someSet_;
};
Run Code Online (Sandbox Code Playgroud)
还可以明确指定比较函数。例如:
#include <set>
auto compare = [](size_t index1, size_t index2) {
return index1 < index2;
};
class Foo
{
public:
Foo() : someSet_(compare)
{
}
private:
std::set<size_t, decltype(compare)> someSet_;
};
Run Code Online (Sandbox Code Playgroud)
现在,假设比较函数是成员函数。例如:
#include <set>
#include <vector>
class Foo
{
public:
Foo() : someSet_(compare) // does not compile
{
}
private:
bool …Run Code Online (Sandbox Code Playgroud) 假设我想处理文件的每一行,但最后一行需要特殊处理:
with open('my_file.txt') as f:
for line in f:
if <line is the last line>:
handle_last_line(line)
else:
handle_line(line)
Run Code Online (Sandbox Code Playgroud)
问题是,如何实施?似乎没有用于在Python中检测文件结尾的功能.
是否有另一种解决方案,而不是将行读入列表(使用f.readlines()或类似)?
假设我想std::vector在C++ 11中创建一个10个指针,每个指针都指向一个默认构造的类实例Foo.这是一种方法:
std::vector<Foo*> foos;
for (int i = 0; i != 10; ++i) {
foos.push_back(new Foo());
}
Run Code Online (Sandbox Code Playgroud)
有没有惯用的方法来避免for循环?
说我们上课Foo:
class Foo {
public:
...
};
Run Code Online (Sandbox Code Playgroud)
Foo有一个实例方法,它将Foo实例转换为另一个Foo实例,或返回相同的Foo实例:
<some appropriate pointer type to Foo> Foo::tryToTransform() {
if (canBeTransformed()) {
<delete this Foo instance>;
return <new instance of Foo>;
}
else {
return <this instance of Foo>;
}
}
Run Code Online (Sandbox Code Playgroud)
客户代码:
<some appropriate pointer type to Foo> foo = ...;
...
foo = foo->tryToTransform();
Run Code Online (Sandbox Code Playgroud)
使用裸指针很容易做到这一点:
Foo* Foo::tryToTransform() {
if (canBeTransformed()) {
delete this;
return new Foo(...);
}
else {
return this;
}
} …Run Code Online (Sandbox Code Playgroud) 在下面的类模板中,我们可以Foo::Bar()为某个类型专门化成员,比如T= int:
template <typename T>
class Foo
{
public:
void Bar()
{
std::cout << "generic Bar()" << std::endl;
}
};
template<>
void Foo<int>::Bar()
{
std::cout << "specialized Bar()" << std::endl;
}
int main()
{
Foo<char> generic_foo;
Foo<int> specialized_foo;
generic_foo.Bar();
specialized_foo.Bar();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
如果类Foo有几个模板参数,我们怎么能这样做:
template <typename T, int N>
class Foo
{
public:
void Bar()
{
std::cout << "generic Bar()" << std::endl;
}
};
// How can we specialize Foo::Bar() for T = int?
int …Run Code Online (Sandbox Code Playgroud) 假设Lua堆栈已设置为即将进行的函数调用(with lua_pcall()).
为了使事情具体化,让我们假设该函数接受一个字符串参数,并且该堆栈不包含该函数下面的任何其他Lua对象.也就是说,堆栈看起来像这样:
Lua stack at 1 = a LUA_TFUNCTION
Lua stack at 2 = a LUA_TSTRING
Run Code Online (Sandbox Code Playgroud)
如何从这样的堆栈中获取函数的名称?
我试过了lua_tostring(),和luaL_getmetafield()(带e= "__name"),但这些功能不能用于此目的.
假设我有一个std::vector<int>:
std::vector<int> v;
[v is initialized]
Run Code Online (Sandbox Code Playgroud)
我想获得v. 有一个算法可以做到这一点:
int max_value = *std::max_element(v.begin(), v.end());
Run Code Online (Sandbox Code Playgroud)
到现在为止还挺好。
现在,假设v包含 10,000,000 个元素,其第 10 个元素等于std::numeric_limits<int>::max()。是std::max_element()要(不必要地)检查 的最后 9,999,990 个元素v,还是要认识到不能有大于 的元素std::numeric_limits<int>::max(),从而在第 10 个元素之后停止?
假设您有两个字符串,并且您想知道一个字符串是否是另一个字符串的前缀.这是在C++中实现它的一种方法:
std::string s = ...;
std::string prefix = ...;
bool sStartsWithPrefix = std::equal(prefix.begin(), prefix.end(), s.begin());
Run Code Online (Sandbox Code Playgroud)
但是假设s您没有输入流而是:
std::istream& inputStream = ...;
std::string prefix = ...;
bool inputStreamStartsWithPrefix = ?
Run Code Online (Sandbox Code Playgroud)
当然,我们可以inputStreamStartsWithPrefix通过编写for循环来计算.但是如何以惯用方式(没有for循环,使用算法)?
在编写 C++ 代码时,而不是:
double a, b;
...
std::complex<double> z = a + b * std::complex<double>(0, 1);
Run Code Online (Sandbox Code Playgroud)
我更愿意写这样的东西:
std::complex<double> z = a + b * i;
Run Code Online (Sandbox Code Playgroud)
我可以看到 C99 有宏I(https://en.cppreference.com/w/c/numeric/complex/I),但它不能用于std::complex:
std::complex<double> z = a + b * I; // does not compile
Run Code Online (Sandbox Code Playgroud)
当然,我可以为此目的定义一些常量,但该常量必须已经存在于 C++ 中。这叫什么?