std

std::unordered_map initialization

心不动则不痛 提交于 2020-06-24 22:08:30
问题 When I access an element in std::unordered_map using operator [] for the first time, it is automatically created. What (if any) are guarantees about its initialization? (It is guaranteed to be value initialized, or only to be constructed)? Example: std::unordered_map<void *, size_t> size; char *test = new char[10]; size[test] += 10; Is size[test] guaranteed to be 10 at the end of this sequence? 回答1: Is size[test] guaranteed to be 10 at the end of this sequence? Yes. In the last line of your

C++: cannot see output in VS Code while debugging

最后都变了- 提交于 2020-06-17 15:49:50
问题 Today I'm trying to switch from VS2019 to VS Code while keep working with MSVC. This way I will develop in a lightweight and easier environment most of the time, and when I need advanced stuff such as seeing hot paths, I would be able to open VS2019 and do the job. Below is my launch.json configuration "name": "debug", "type": "cppvsdbg", "request": "launch", "program": "${workspaceFolder}/build/${fileBasenameNoExtension}.exe", "args": [], "stopAtEntry": false, "cwd": "${workspaceFolder}",

Why to use 'errno' at all?

与世无争的帅哥 提交于 2020-06-14 05:56:37
问题 I'm a cs student at the Technion, I have just learned of 'errno' variable and c-style func. call. This makes me wonder, if c-style syscall uses registers to return a value, why should anyone use 'errno' at all? 回答1: The main reason for using errno is to give more information about the error condition. This is especially useful in situations where most (or even all ) possible return values of a function are actually valid return values. Consider the fopen() function, which returns a pointer to

Why to use 'errno' at all?

自闭症网瘾萝莉.ら 提交于 2020-06-14 05:56:06
问题 I'm a cs student at the Technion, I have just learned of 'errno' variable and c-style func. call. This makes me wonder, if c-style syscall uses registers to return a value, why should anyone use 'errno' at all? 回答1: The main reason for using errno is to give more information about the error condition. This is especially useful in situations where most (or even all ) possible return values of a function are actually valid return values. Consider the fopen() function, which returns a pointer to

Is vector::shrink_to_fit allowed to reallocate?

瘦欲@ 提交于 2020-06-11 17:50:18
问题 This member function, that has no defined effect in the standard (only remarks), would have limited use if not allowed to reallocate. But the only paragraph I found in the standard that seems to apply would be 23.2.1/11: "Unless otherwise specified (either explicitly or by defining a function in terms of other functions), invoking a container member function or passing a container as an argument to a library function shall not invalidate iterators to, or change the values of, objects within

Is vector::shrink_to_fit allowed to reallocate?

北战南征 提交于 2020-06-11 17:50:10
问题 This member function, that has no defined effect in the standard (only remarks), would have limited use if not allowed to reallocate. But the only paragraph I found in the standard that seems to apply would be 23.2.1/11: "Unless otherwise specified (either explicitly or by defining a function in terms of other functions), invoking a container member function or passing a container as an argument to a library function shall not invalidate iterators to, or change the values of, objects within

Remove trailing comma in CSV file written for a vector using copy and ostream_iterator

爷,独闯天下 提交于 2020-06-08 20:01:10
问题 I have the following function, which writes a vector to a CSV file: #include <math.h> #include <vector> #include <string> #include <fstream> #include <iostream> #include <iterator> using namespace std; bool save_vector(vector<double>* pdata, size_t length, const string& file_path) { ofstream os(file_path.c_str(), ios::binary | ios::out); if (!os.is_open()) { cout << "Failure!" << endl; return false; } os.precision(11); copy(pdata->begin(), pdata->end(), ostream_iterator<double>(os, ",")); os

Why can I not pass this comparison function as a template argument?

只愿长相守 提交于 2020-06-02 10:57:22
问题 I am trying to create an std::set with a function I defined for sorting, but I get the error: "Error: function "GFX::MeshCompare" is not a type name" Mesh.h namespace GFX { struct Mesh { [...] }; inline bool MeshCompare(const Mesh& a, const Mesh& b) { return ( (a.pTech < b.pTech) || ( (b.pTech == a.pTech) && (a.pMaterial < b.pMaterial) ) || ( (b.pTech == a.pTech) && (a.pMaterial == b.pMaterial) && (a.topology < b.topology) ) ); } }; Renderer.h namespace GFX { class Renderer { private: [...]

Why can I not pass this comparison function as a template argument?

允我心安 提交于 2020-06-02 10:50:35
问题 I am trying to create an std::set with a function I defined for sorting, but I get the error: "Error: function "GFX::MeshCompare" is not a type name" Mesh.h namespace GFX { struct Mesh { [...] }; inline bool MeshCompare(const Mesh& a, const Mesh& b) { return ( (a.pTech < b.pTech) || ( (b.pTech == a.pTech) && (a.pMaterial < b.pMaterial) ) || ( (b.pTech == a.pTech) && (a.pMaterial == b.pMaterial) && (a.topology < b.topology) ) ); } }; Renderer.h namespace GFX { class Renderer { private: [...]

Why can I not pass this comparison function as a template argument?

大兔子大兔子 提交于 2020-06-02 10:46:11
问题 I am trying to create an std::set with a function I defined for sorting, but I get the error: "Error: function "GFX::MeshCompare" is not a type name" Mesh.h namespace GFX { struct Mesh { [...] }; inline bool MeshCompare(const Mesh& a, const Mesh& b) { return ( (a.pTech < b.pTech) || ( (b.pTech == a.pTech) && (a.pMaterial < b.pMaterial) ) || ( (b.pTech == a.pTech) && (a.pMaterial == b.pMaterial) && (a.topology < b.topology) ) ); } }; Renderer.h namespace GFX { class Renderer { private: [...]