任何人都可以解释一下记忆的布局
std::vector<std::array<int, 5>> vec(2)
Run Code Online (Sandbox Code Playgroud)
它是否提供具有2行5个元素的2D数组的连续内存块?
据我所知,矢量矢量
std::vector<std::vector<int>> vec(2, std::vector<int>(5))
Run Code Online (Sandbox Code Playgroud)
提供存储器中不同位置的两个 长度为 5个元素的连续数组的存储器布局.
对于数组的向量是否相同?
当移动-构造std::function从一个对象的λ,其中该拉姆达具有由值捕获,看来该物体的移动,构造函数,是值捕获被调用两次。考虑
#include <功能>
#include <iostream>
结构体
{
整数值 = 1;
Foo() = 默认值;
Foo(const Foo &) {}
富(富&&)
{
std::cout << "移动构造函数" << std::endl;
}
};
int main()
{
福福;
自动 lambda = [=]() { 返回 foo.value; };
std::cout << "---------" << std::endl;
std::function<int()> func(std::move(lambda));
std::cout << "---------" << std::endl;
返回0;
}
输出是
---------
move ctor
move ctor
---------
Run Code Online (Sandbox Code Playgroud)
我在 Mac OS X Catalina 上工作,我的编译器是
g++-9 (Homebrew GCC 9.3.0) 9.3.0
Run Code Online (Sandbox Code Playgroud)
我用g++ -std=c++17. …
如何std::vector<std::pair<int, int>>在任一轴上找到这对中的max元素。
将此作为样本对:
0, 1
0, 2
1, 1
1, 2
1, 4
2, 2
3, 1
Run Code Online (Sandbox Code Playgroud)
我尝试使用std::minmax_element():
const auto p = std::minmax_element(edges.begin(), edges.end());
auto max = p.second->first;
Run Code Online (Sandbox Code Playgroud)
但这仅生成第一列3的最大元素,即,但我想要任一列的最大元素,即4。
我希望max元素成为任一列中的最高元素。
我喜欢在列表上编写函数检查.为此我通常写一个这样的函数:
inline bool good_strings(const std::vector<const char *> & items)
{
for (i in items) {
if (not is_good(i)) return false;
}
return true;
}
Run Code Online (Sandbox Code Playgroud)
然后我可以写得像if (all_good({"a", "b", "c", "d", "e"})) {...},它看起来非常好.当您对几件物品的检查变得更大时,这很好用:
if (is_good("a") and is_good("b") and /* that's too much, man */ is_good("c")) {...}
Run Code Online (Sandbox Code Playgroud)
但我关心的是我使用的容器的开销,也很难选择一个:std::vector,std::list,QList,QStringList或者甚至std::array或std::initializer_list- 应该使用内联函数?在使用括号创建时,这些中的哪一个具有最小甚至零开销{}?
好的,并且更新:我抓住了我的朋友许可的IDA Pro并检查了一些选项.
std::initializer_list:该函数甚至没有内联,并且存在创建列表和复制指针的开销.std::vector:函数内联,但是,创建向量和复制指针存在开销.std::array:由于模板特化而不是很好看,而且函数没有内联.因此,多次调用会创建许多类似的代码块.但是,没有数组创建的开销,并且所有指针都作为函数参数传递,这对于x86_64寄存器调用约定来说很快.问题仍然存在,是否有一个绝对零成本的容器?
可以很容易地std::string_view从 a创建一个std::string。但是如果我想创建一个范围的字符串视图,std::string使用迭代器是std::string行不通的。
这是我尝试过的代码:https : //gcc.godbolt.org/z/xrodd8PMq
#include <iostream>
#include <string>
#include <string_view>
#include <iterator>
int main()
{
std::string str{"My String"};
std::string_view strView{str}; // works
//std::string_view strSubView{str.begin(), str.begin() + 2}; // error
}
Run Code Online (Sandbox Code Playgroud)
当然,也许我们可以将子字符串制作出来str并用于制作字符串视图strSubView,但是还有一个额外的字符串创建。
我发现std::basic_string_view第 5 个构造函数采用迭代器的范围。
template<class It, class End>
constexpr basic_string_view(It first, End last);
Run Code Online (Sandbox Code Playgroud)
但它仅仅是std::stringor std::basic_string_view本身的迭代器吗?如果不是 forstd::string迭代为什么我们不应该有一个,毕竟是字符串视图:
描述了一个对象,它可以引用一个常量连续的类似字符的对象序列!
取char连续序列的范围,不应该算吗?
我编写了一个程序来查看如何在模板函数中推导出字符串文字.
#include <iostream>
#include <string>
#include <type_traits>
template<typename T> void passByValue(T by_value)
{
std::cout << std::is_same_v<char const*, decltype(by_value)> << std::endl; // okay
}
template<typename T> void passByReferance(T &by_ref)
{
std::cout << std::is_same_v<char const*, std::remove_reference_t<decltype(by_ref)>> << std::endl;
}
template<typename T> void passByConstRef(const T &const_ref)
{
std::cout << std::is_same_v<char const*, std::remove_const_t<std::remove_reference_t<decltype(const_ref)>>> << std::endl;
}
int main()
{
std::cout << std::boolalpha;
passByValue("string"); // true: good
passByReferance("string");// false ??
passByConstRef("string"); // false ??
return 0;
}
Run Code Online (Sandbox Code Playgroud)
事实证明,只有passByValue推断出字符串文字才能const char*输入. …
我在cppreference.com上读过新的(自C++ 17以来)std::vector::emplace_back对插入元素的referance返回值.
返回值
- (无)(直到C++ 17)
- 对插入元素的引用.(自C++ 17起)
我在想,在将元素插入向量时,为什么我们需要对它进行反馈?这可能是多么有用或者这个新回归的用例是什么?
这是我编写的示例代码,该功能.
#include <vector>
int main()
{
std::vector<int> myVec;
for(int i = 0; i < 3; ++i)
{
int& newElement = myVec.emplace_back(i);
^^^^^^^ => why standard should expose the element after inserting.
}
}
Run Code Online (Sandbox Code Playgroud) 我有以下模板类,其中成员是const ref类型。对象的复制被禁用,并且只需要移动对象和移动赋值运算符。
Q1:如何const ref type正确实现移动赋值运算符(我所做的是否正确)?
Q2:为什么会这样
MyClass<int> obj2(std::move(obj)); // will work with move ctor
MyClass<int> obj3 = std::move(obj2); // also move ctor called: Why?
Run Code Online (Sandbox Code Playgroud)
发生了?
Q3:在main()移动实例中是否可以调用using print()。是UB吗?
我正在使用Visual Studio 2015 (v140)。这是我的代码:
#include <utility>
#include <iostream>
template<typename Type>
class MyClass
{
const Type& m_ref; // const ref type
public:
explicit MyClass(const Type& arg): m_ref(std::move(arg)){}
// coping is not allowed
MyClass(const MyClass&) = delete;
MyClass& operator=(const MyClass&) = delete;
// …Run Code Online (Sandbox Code Playgroud) 我正在尝试 C++17 功能std::optional
可选的返回类型是std::optional<std::pair<int, int>>. 我在函数中调用该
sum_pair函数print_answer并想要一个可选的打印。
在print_answer函数中,我想检查所需的对是否包含要显示的内容。就像下面给出的示例一样:可选返回工厂函数可用作 while 和 if 的条件
以下是代码:这里有错误
#include <iostream>
#include <vector>
#include <unordered_map>
#include <optional>
typedef std::optional<std::pair<int, int>> returnType;
// following algorithum works fine: just to show,
// how I have used the std::optional
returnType sum_pair(const std::vector<int>& vec, const int sum)
{
std::unordered_map<int, int> compIndexMap;
int index = 0;
for(const int& ele: vec)
{
if(auto check = compIndexMap.find(sum - ele); check != compIndexMap.cend())
return …Run Code Online (Sandbox Code Playgroud) 我无法弄清楚,为什么C ++不允许根据返回类型进行重载,因为在以下情况下,三个member(getter)函数具有不同的函数签名,即使在存储指向成员函数的指针时,我们也需要不同的mem-函数指针类型如下:
for instance T = std::string
using constRefPtr = const std::string&(MyStruct::*)() const;
using constValuePtr = const std::string(MyStruct::*)() const;
using valuePtr = std::string(MyStruct::*)() const;
Run Code Online (Sandbox Code Playgroud)
我已经阅读过这篇类似的文章,建议使用const和非成本成员函数。
问题:如何在不删除const每个成员函数的本质的情况下使以下(getter)重载工作(如果可以通过标准C ++实现)?
我正在使用C ++ 17。
#include <iostream>
#include <string>
template<typename T> class MyStruct
{
T m_val;
public:
explicit MyStruct(const T& value)
: m_val(value)
{}
const T& getVal() const { return m_val; } // get val as const ref(no copy of member)
const T getVal() const { return m_val; } // get …Run Code Online (Sandbox Code Playgroud) c++ ×10
c++17 ×5
c++11 ×3
stdvector ×3
algorithm ×1
emplace ×1
if-statement ×1
inline ×1
lambda ×1
overhead ×1
overloading ×1
std ×1
std-function ×1
stdarray ×1
stdoptional ×1
stdstring ×1
stl ×1
string-view ×1
templates ×1