stl

How to iterate through a STL set till the second last element?

这一生的挚爱 提交于 2021-01-28 08:11:19
问题 I want to iterate through a STL set to do operations on elements pairwise in the set. For eg. if set S={1,2,3), I need to be able to check {1,2},{2,3},{1,3}. So a very wrong C++ code for this would be as follows- set<bitset<2501> > arr; unsigned sz = arr.size(); rep(i,sz-1) { for(j=i+1;j<sz;j++) { //do processing and in my case is an OR operation if(((arr[i])|(arr[j])) == num) { cnt++; } } } I wrote the above wrong code to give you a better idea of what I want to do. A better version (should

ifstream operator >> uint16_t sets failbit

别说谁变了你拦得住时间么 提交于 2021-01-28 06:35:21
问题 i'm trying to ready a binary-file into a set of variables using the c++ std::ifstream class. The following example works: std::ifstream inFile; inFile.open("example.bin"); uint8_t temp8; uint16_t temp16; inFile >> temp8; inFile >> temp8; But if i replace the last two lines with one line inFile >> temp16; nothing is read and inFile.fail() returns true . Can anyone explain, why I can't read into a 16 bit variable? 回答1: The operator>> overload for reading uint16_t from istreams is a formatted

Linking errors while exporting std::vector from dll

邮差的信 提交于 2021-01-28 04:55:15
问题 I have a dll ( my_library.dll ) that exports a struct using __ declspec(dllexport) . Since this struct contains an std::vector<std::wstring> member , I've also exported functions for it like so: template class __declspec(dllexport) std::allocator<std::wstring>; template class __declspec(dllexport) std::vector<std::wstring>; Please note that I've defined macros such that dll exports above struct and vector when compiling and they are imported (via __declspec(dllimport)) when the dll is being

How to convert Three.js to .stl files for 3D printing?

若如初见. 提交于 2021-01-28 02:37:58
问题 I found one page link: Convert Three.js to .stl for 3D printing? var exporter = new THREE.STLExporter(); var str = exporter.parse(scene); console.log(str); But when I'm using them, not export a stl file. How can I do then? 回答1: The STLExporter.parse() will export the specified object to a string. If you wish to save the string as a file you have to perform some more operations. As a simple approach you can use FileSaver.js for saving the string to a file. First of all you have to download and

Why does this use of std::transform cause exc_bad_access

好久不见. 提交于 2021-01-28 00:57:54
问题 I have this code (roughly) in a cpp app: QList<Foo> rawAssets = getlist(); QVector<std::pair<QString, QString>> assets; std::transform(rawAssets.begin(), rawAssets.end(), assets.begin(), makePair); That causes exc_bad_access to throw when I use assets again. However, if I change assets.begin() to std::back_inserter(assets) then it works as I expect. I found tutorials for std::transform showing both kinds of usage. Why is it wrong in my case? 回答1: Since you've just declared QVector<std::pair

special sorting algorithm and generic signature

南楼画角 提交于 2021-01-27 18:48:09
问题 I have a strong use-case to define my own sorting algorithm, which is faster than the fastest in stl and by exploiting some nice properties of the underlying data I basically can sort in O(n) . So far so good, now the problem is that I would like to offer a generic interface which will fit any type of container e.g. T* or std::vector<T> etc, as long as couple of key concepts apply e.g. there is a valid operator [] available to access the elements of the collection the elements of the

Initializing a member variable STL vector through a class constructor

跟風遠走 提交于 2021-01-27 18:04:17
问题 I have the following code which seems to work: class MapCell { public: int x, y, z; }; void Test3DVector(int size_x, int size_y, int size_z) { vector< vector< vector<MapCell> > > m(size_x, vector<vector<MapCell>>(size_y, vector<MapCell>(size_z))); for (int x = 0; x < size_x; ++x) { for (int y = 0; y < size_y; ++y) { for (int z = 0; z < size_z; ++z) { m[x][y][z].x = x; m[x][y][z].y = y; m[x][y][z].z = z; } } } } I want to write a class that has the 3D vector as a member variable, with the

iterator invalidation in map C++

ぐ巨炮叔叔 提交于 2021-01-27 14:18:32
问题 I have a sample program in which I am trying to see how the iterator invalidates while deleting the elements from a map. The program is here: #include <iostream> #include <map> using namespace std; int main(int argc, char *argv[]) { map<int, int> myMap; myMap.insert(pair<int, int>(0, 2)); myMap.insert(pair<int, int>(1, 4)); myMap.insert(pair<int, int>(3, 18)); myMap.insert(pair<int, int>(2, 20)); map<int, int>::iterator it; for(it = myMap.begin(); it != myMap.end(); ++it) { myMap.erase(it); /

Template distinguishing between maps and sets

半城伤御伤魂 提交于 2021-01-27 13:10:41
问题 In creating a code common for set , unordered_set , map , and unordered_map , I need the few methods, where the handling is actually different. My problem is getting the compiler to deduce, which implementation to use. Consider the example: #include <map> #include <unordered_set> #include <string> #include <iostream> using namespace std; static unordered_set<string> quiet; static map<const string, const string> noisy; template <template <typename ...> class Set, typename K> static void insert

pretty print not working for c++ stl list

跟風遠走 提交于 2021-01-27 12:24:56
问题 While using gdb for the following code, it appears that pretty print works as expected for stl vector and strings but the output for list seems cryptic. list<int> l; string a = "Hello"; vector<int> v(2,3); l.push_back(5); l.push_back(10); Output of gdb pretty print: (gdb) set print pretty on (gdb) disp v 1: v = std::vector of length 2, capacity 2 = {3, 3} (gdb) disp a 2: a = "Hello" (gdb) disp l 3: l = { <std::__cxx11::_List_base<int, std::allocator<int> >> = { _M_impl = { <std::allocator<std