c++

How reserve in std::vector works + Accessing vector with []

无人久伴 提交于 2021-02-20 04:44:07
问题 Why vector[n] = val doesn't give segmentation fault or changes the vector data, right before reserving an empty vector. Check this example: #include <iostream> #include <vector> int main() { std::vector<int> temp; temp.reserve(8); temp[0] = 1; temp[3] = 3; //why no attribution??? temp[7] = 1; temp[8] = 3; //why no segmentation fault??? std::cout << temp.size(); for(auto&a: temp){ //because the attribution didn't work, no loop needed std::cout << a; } return 0; } Also, why the operator []

Link errors using Homebrew's Boost::Log in OSX El Capitan

六眼飞鱼酱① 提交于 2021-02-20 04:39:11
问题 OSX 10.11.3 (El Capitan) I'm getting link errors while building the first example from the Boost::Log tutorial. #include <boost/log/trivial.hpp> int main(int, char*[]) { BOOST_LOG_TRIVIAL(trace) << "A trace severity message"; BOOST_LOG_TRIVIAL(debug) << "A debug severity message"; BOOST_LOG_TRIVIAL(info) << "An informational severity message"; BOOST_LOG_TRIVIAL(warning) << "A warning severity message"; BOOST_LOG_TRIVIAL(error) << "An error severity message"; BOOST_LOG_TRIVIAL(fatal) << "A

Techniques used in checking if a binary tree is symmetric

我只是一个虾纸丫 提交于 2021-02-20 04:28:33
问题 Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). Question link is here The recursion method need to traverse the tree twice. But one of the comment provided a solution used a technique called 'Null check'. I can't understand why in this way can we avoid checking the tree twice? Here is his code in C++: bool isSymmetric(TreeNode* root) { if (!root) return true; return isSymmetric(root->left, root->right); } bool isSymmetric(TreeNode* t1, TreeNode*

知乎万赞:计算机应届生月薪大多是多少?

て烟熏妆下的殇ゞ 提交于 2021-02-20 04:27:05
注:这个回答是我一位朋友在知乎上的万赞匿名回答,取得了他的同意,特此在这里转发,并写下一点自己的感想。 (但是,想了下,由于这个回答是匿名的,似乎也很难证明这个回答就是我朋友的。。。但是我让他在回答下帮我置顶了一条的评论,应该勉强算一个证明了哈哈哈) 问题: 计算机应届生月薪大多是多少? 月薪4k,6k,8k,10k的各占百分之多少呢,想了解一下知乎上大家的就业情况。好对自己有个定位,亲身经历或者朋友同学什么的都可以说说哈。 我的朋友回答: 能做到以下几点,绝对可以在北上杭深拿到15K(小北注:这是18年的回答了)以上: 撒比算法题,各大公司笔试面试基本都考这个,别的不说,《剑指offer》所有题目背下来,leetcode刷个四五百题起码的吧。搞过ACM也可以,反正刷个四五百题是必须的。我也觉得考这些撒比算法题很撒比,但是大家都这么考,你不准备,那不挂你挂谁? 数据结构,不说要彻底给面试官手推各种数据结构的均摊复杂度,也不要求你手写红黑树,至少AVL 哈希表 堆这些简单的数据结构得自己实现过;链表、线性表必须熟悉到不能再熟悉,比如vector为什么要用加倍扩容而不是每次增加一个固定的扩容容量;BBST各种都能扯一下,达到这个程度就差不多了。 计算机组成原理, 至少《深入理解计算机系统》得过一遍吧?配套实验也得做一下吧? 计算机网络,这个最简单了,随便找本书,把应用层、传输层看完就行

How to prevent GetOpenFileName from changing the current directory while the dialog is shown?

为君一笑 提交于 2021-02-20 04:26:06
问题 GetOpenFileName (for questionable reasons) changes the current directory of an application while the dialog is shown. This can be reset on dialog closure by specifying OFN_NOCHANGEDIR as dialog initialization flag: OFN_NOCHANGEDIR Restores the current directory to its original value if the user changed the directory while searching for files. Setting this flag, however, doesn't prevent the function from changing the current directory while the explorer dialog is shown . This is an issue in

Why does `auto` not adopt the constexpr'ness of its initializing expression?

孤人 提交于 2021-02-20 04:23:05
问题 Why doesn't defining a variable with auto keyword carry the constexpr 'ness of the expression used to initialize it? As an example, consider the following code: #include <string_view> constexpr std::string_view f() { return "hello"; } static constexpr std::string_view g() { constexpr auto x = f(); // (*) return x.substr(1, 3); } int foo() { return g().length(); } With GCC 10.2 and --std=c++20 -fsanitize=undefined -O3 , this compiles into: foo(): mov eax, 3 ret But if we remove the constexpr

OpenPose linking error on std::thread

▼魔方 西西 提交于 2021-02-20 04:21:25
问题 So trying to build OpenPose from source, and was running into a linking problem. Current environment is Ubuntu 16.04.4. OpenCV version is 3.3.1. GCC version is 5.4.0. [ 87%] Built target openpose [ 87%] Linking CXX executable 1_extract_from_image.bin [ 87%] Linking CXX executable openpose.bin [ 87%] Linking CXX executable 3_user_input_processing_and_output.bin [ 87%] Linking CXX executable 2_extract_pose_or_heatmat_from_image.bin [ 87%] Linking CXX executable 1_custom_post_processing.bin [ 87

Nested class access to enclosing class members

六眼飞鱼酱① 提交于 2021-02-20 04:14:33
问题 From C++ Primer 5th edition By Stanley Lippman et al(19.5): A nested class can have the same kinds of members as a nonnested class. Just like any other class, a nested class controls access to its own members using access specifiers. The enclosing class has no special access to the members of a nested class, and the nested class has no special access to members of its enclosing class . Is there any truth to the bolded part? I could find no mention of the nested class being restricted access

Nested class access to enclosing class members

泄露秘密 提交于 2021-02-20 04:12:54
问题 From C++ Primer 5th edition By Stanley Lippman et al(19.5): A nested class can have the same kinds of members as a nonnested class. Just like any other class, a nested class controls access to its own members using access specifiers. The enclosing class has no special access to the members of a nested class, and the nested class has no special access to members of its enclosing class . Is there any truth to the bolded part? I could find no mention of the nested class being restricted access

Eclipse IDE make: *** No rule to make target… needed by 'core/abi.cpp.o'. Stop

回眸只為那壹抹淺笑 提交于 2021-02-20 04:12:16
问题 I'm trying to make Arduino projects with eclipse IDE and I have followed this video tutorial and I am getting this error: Building ArduinoTest make: *** No rule to make target 'Ulloa/.arduinocdt/packages/arduino/hardware/avr/1.6.19/cores/arduino/abi.cpp', needed by 'core/abi.cpp.o'. Stop. I'm new with this and I don't know what I could be doing wrong. This is my makefile, it's the one that was generated by Eclipse: ifeq ($(OS),Windows_NT) SHELL = $(ComSpec) RMDIR = rmdir /s /q RM = del /q