
[英]How to implode a vector of strings into a string (the elegant way)
我正在寻找将字符串向量内爆为字符串的最优雅的方法。 下面是我现在使用的解决方案: 还有其他人吗? ...
[英]How to implode a vector of strings into a string (the elegant way)
我正在寻找将字符串向量内爆为字符串的最优雅的方法。 下面是我现在使用的解决方案: 还有其他人吗? ...
[英]Adding an item to the vector without creating a temporary object
编译器会优化这段代码,而不是创建一个临时字符串 object 吗? ...
[英]Is the size of std::array defined by standard
在 C++11 中, std::array被定义为具有不比数组差的连续存储和性能,但我无法确定标准的各种要求是否暗示 std::array 具有相同的大小和 memory 布局作为正常数组。 那就是你能指望sizeof(std::array<int,N>) == sizeof(int)* ...
[英]How to retrieve all keys (or values) from a std::map and put them into a vector?
这是我出来的可能方式之一: 当然,我们也可以通过定义另一个仿函数RetrieveValues来检索 map 中的所有值。 有没有其他方法可以轻松实现这一目标? (我一直想知道为什么std::map不包含一个成员 function 让我们这样做。) ...
[英]std::deque: How do I get an iterator pointing to the element at a specified index?
我有一个 std::deque,我想在指定的索引处插入一个元素(我知道 std::list 在这方面会更好)。 deque::insert() function 采用迭代器来指定要插入的位置。 给定一个索引,我怎样才能得到一个指向该位置的迭代器,以便我可以将该迭代器传递给 insert()? 例 ...
[英]Best practice for overloading functions with only STL types
我想重载operator<<(std::ostream&, std::chrono::duration<std::uint64, std::nano>) - 理想情况下不需要用户在任何地方编写using语句。 这里的最佳做法是什么? 语境... 有没有一种方法可以在不 ...
[英]How can I approach this CP task?
任务(来自保加利亚法官,点击“Език”将其更改为英语): 我得到了 N 种珊瑚中第一个 (S 1 = A) 的大小。 每个后续珊瑚的大小(S i ,其中 i > 1)使用公式 (B*S i-1 + C)%D 计算,其中 A、B、C 和 D 是一些常数。 我被告知 Nemo 在第 K个珊瑚附 ...
[英]c++ Overloaded operator () change std::set.find()
我正在学习 std::set。 这是我的代码: Output: 1 我使用 struct cmp 来自定义元素的排序规则,如果 abs(ab)<=3 新元素将被删除。 但是让我吃惊的是 q.find() 已经改变了。 我想知道为什么output是1,q里面没有4。 q.find(4) 是 ...
[英]Is there a way to pass variables to std::make_tuple<double, char, std::string>(var1, var2)?
我想将变量名传递给 std::make_tuple(),但它不允许。 我正在使用 C++14,有没有办法实现我想要的? 这会抛出一条错误消息“无法将‘int&&’类型的右值引用绑定到‘int’类型的左值” ...
[英]!st.empty() is throwing runtime error if kept after &&
while(.st.empty() && nums[i%n] >= st.top()) st;pop(); 这段代码有效但是while(nums[i%n] >= st.top() &&.st.empty()) st;pop(); 这不是! 我正在解决 le ...
[英]Is it possible to create an STL container where the contents are mutable, but the container's attributes aren't?
这可能最容易用例子来解释。std::vector<int> a; a.push_back(1); a[0] = 2; const std::vector<int>& b = a; b.push_back(1); // Throws an error I want. ...
[英]Finding a key-value-pair within a std::multimap with key of type int
我有一个std::multimap<int, X*> ,其中X是用户定义的类型。 我想在此多重映射中找到特定的键值对(即指向此对的迭代器)。 (A) 完整示例: 但是,这不会编译(带有-std=gnu++2a的 gcc 12.2.1): 所以在我看来int以某种方式转换为const i ...
[英]How a multiset works and how one can find the minimum element in multiset
当我们在多重集中插入元素时,它们是按排序顺序插入的。 我怎样才能找到 mutiset 的最小元素? 我如何访问 mutiset 中的第 i 个元素? 有人可以解释一下 multiset 是如何工作的以及它如何在其中存储元素吗? 提前致谢。 ...
[英]segmentation fault after popping the element that has been pointed to by
我发现这段代码可能会导致分段错误。 因为ptr最终指向弹出的元素。 但我不知道发生这种情况的确切原因。 我只是假设pop操作在内部为弹出的 memory 解除分配。 我的推测对吗? 还是其他原因? ...
[英]Custom Iterator in C++
我有一个 class TContainer,它是几个 stl collections 指向 TItems class 的指针的集合。 我需要创建一个迭代器来遍历我的 TContainer class 中所有 collections 中的元素,抽象出内部工作的客户端。 这样做的好方法是什么? 我是否应 ...
[英]What actually is done when `string::c_str()` is invoked?
调用string::c_str()时实际做了什么? string::c_str()会分配memory,将字符串object和append的内部数据复制一个空字符到新分配的memory? 或者由于string::c_str()必须是 O(1),因此不再允许分配 memory 并复制string 。 ...
[英]Custom C++ Allocator cause error in : _Container_base12::_Orphan_all_unlocked_v3()
所以我打算将这个基于堆栈的分配器用于 std::vector,并且我使用 2 arrays 进行分配(因为向量增长并将旧缓冲区复制到新缓冲区)。 这是完整的代码: 代码以某种方式导致崩溃:在此处输入图像描述在此处输入图像描述我注意到“_Container_base12”继承自分配器,所以我想我的“ ...
[英]C++ deque STL - Maximum of contiguous subarray
这是一个 hakerrank 问题给定一组 arrays 的大小和一个 integer,您必须为每个给定的 arrays 的每个连续子数组找到最大的 integer。 输入格式输入的第一行将包含测试用例的数量 T。对于每个测试用例,您将获得数组 N 的大小和要使用的子数组的大小 K。接下来是数组 A ...
[英]Iterate over all elements with key k in a std::unordered_multimap in O(count(k))
我有一个std::unordered_multimap ,并且想遍历具有给定键 k 的所有元素,而不遍历完整的 map,但最好只遍历匹配的项目。 虽然我可以在有序的std::multimap中使用 upper_bound 来做到这一点,但我无法在任何地方找到find()后跟前向迭代直到键不同,将遍历 ...