stl

Is it possible for std::multimap::equal_range to return incorrect results?

前提是你 提交于 2020-01-05 14:17:49
问题 Good afternoon, I am finding that std:multimap::equal_range returns incorrect results sometimes. Is this possible? If so, is there a workaround or some error in my code or hash function for pointers. Thank you. Here is an excerpt of my code: typedef std::multimap<char *,Range>::const_iterator I; std::pair<I,I> b = mmultimap.equal_range(TmpPrevMapPtr); for (I i=b.first; i != b.second; ++i){ ranges_type.erase(i->second); } erasecount = mmultimap.erase(TmpPrevMapPtr); where mmultimap has a

Memory corrupt in adding string to vector<string> loop

我们两清 提交于 2020-01-05 13:25:30
问题 This is on Visual Studio 2008 on a dual-core, 32 bit Vista machine. In the debug code this runs fine, but in Release mode this bombs: void getFromDB(vector<string>& dates) { ... sql::Resultset res = stmt->executeQuery("SELECT FROM ..."); while (res->next()) { string date = res->getString("date"); dates.push_back(date); } // <<< crashing here (line 56) delete res; } The MySQL C++ connector has this method in it's ResultSet: virtual std::string getString(const std::string& columnLabel) const =

Can i use xml3d with stl models?

两盒软妹~` 提交于 2020-01-05 08:36:12
问题 I'd really like to use STL models with XML3d. Is there a way to convert back and forth between these formats? 回答1: You can convert STL files to XML3D using the Blender exporter for instance: https://github.com/ksons/xml3d-blender-exporter Another option is to write a plug-in for STL files that allow to reference STL files from <mesh> and <data> elements: <mesh src="foo/bar.stl"/> This would be very similar to the existing plug-ins for MeshLab/JSON and OpenCTM files: http://xml3d.github.io

Can i use xml3d with stl models?

喜欢而已 提交于 2020-01-05 08:36:10
问题 I'd really like to use STL models with XML3d. Is there a way to convert back and forth between these formats? 回答1: You can convert STL files to XML3D using the Blender exporter for instance: https://github.com/ksons/xml3d-blender-exporter Another option is to write a plug-in for STL files that allow to reference STL files from <mesh> and <data> elements: <mesh src="foo/bar.stl"/> This would be very similar to the existing plug-ins for MeshLab/JSON and OpenCTM files: http://xml3d.github.io

std::unordered_map with custom value type, operator[]

自作多情 提交于 2020-01-05 08:06:46
问题 I'm trying to use std::unordered_map, as shown in the example here. class CSVRecord { public: CSVRecord(string csvLine) : _fields(vector<string>()) {...} vector<string> _fields; }; int main(int argc, char* argv[]) { unordered_map<string, CSVRecord> m; CSVRecord rec = CSVRecord("test"); m["t"] = rec; return 0; } However, m["t"] = rec gives an error: no matching function for call to ‘CSVRecord::CSVRecord()’ . I used m.insert(pair<string, CSVRecord>("t",rec)) instead, but I wonder why the

C++ class method, returns vector<subclass>

六月ゝ 毕业季﹏ 提交于 2020-01-05 07:51:10
问题 I'm having a bit of trouble with a method I'm trying to write for a class. I have class symbol and class terminal. class terminal extends class symbol, but one of the methods of class symbol needs to return a vector. E.g.: #ifndef SYMBOL_H #define SYMBOL_H #include "terminal.h" #include <vector> using namespace std; class symbol { public: vector<terminal> first(); virtual void polymorphable(); }; #endif With class terminal defined: #ifndef TERMINAL_H #define TERMINAL_H #include "symbol.h"

Stack Overflow With Unordered Map

China☆狼群 提交于 2020-01-05 07:15:12
问题 For a project, I have an opening table that I decided to put in a std::unordered_map . Unfortunately, I am restricted to hard-coding the entire map. So, I decided to split the initialization into multiple files. class OpeningBook { public: OpeningBook(); private: std::unordered_map<std::string, int> opening_database_; void init1(); void init2(); void init3(); void init4(); void init5(); }; and the constructor just calls the init functions: OpeningBook::OpeningBook() { init1(); init2(); init3(

转:STL提供了三个最基本的容器:vector,list,deque

狂风中的少年 提交于 2020-01-05 05:41:00
vector和built-in数组类似,它拥有一段连续的内存空间,并且起始地址不变,因此它能非常好的支持随机存取,即[]操作符,但由于它的内存空间是连续的,所以在中间进行插入和删除会造成内存块的拷贝,另外,当该数组后的内存空间不够时,需要重新申请一块足够大的内存并进行内存的拷贝。这些都大大影响了vector的效率。 list就是数据结构中的双向链表(根据sgi stl源代码),因此它的内存空间可以是不连续的,通过指针来进行数据的访问,这个特点使得它的随机存取变的非常没有效率,因此它没有提供[]操作符的重载。但由于链表的特点,它可以以很好的效率支持任意地方的删除和插入。 deque是一个double-ended queue,它的具体实现不太清楚,但知道它具有以下两个特点:它支持[]操作符,也就是支持随即存取,并且和vector的效率相差无几,它支持在两端的操作:push_back、push_front、pop_back、pop_front等,并且在两端操作上与list的效率也差不多。 因此在实际使用时,如何选择这三个容器中哪一个,应根据你的需要而定,一般应遵循下面的原则: 1、如果你需要高效的随即存取,而不在乎插入和删除的效率,使用vector 2、如果你需要大量的插入和删除,而不关心随即存取,则应使用list 3、如果你需要随即存取,而且关心两端数据的插入和删除,则应使用deque

STL vector size and capacity

爱⌒轻易说出口 提交于 2020-01-05 05:27:24
1. size = m_finish-m_start: 表示此时vector中有多少个元素。 2. capacity: m_end_storage-m_start:表示再不重新分配内存的前提下,可以容纳的元素的个数,注意这个数值包含了已经存在的元素个数,即 capacity()-size():表示还有多少个可以push的元素个数在不重新分配的前提内存的前提下。 3. STLport库元素增长是以当前元素的个数*2的倍数来进行分配的。 4. Resize(int n):n表示要resize到的个数. 如果n<size(),从vector的末尾把size()-n的元素destroy掉(invoke destroy function),使得size()=n。  如果n>size(),重新分配内存并copy已存的元素,所以会发生iterator失效的问题。 5. Reserve(int n): n表示要预留的元素个数。如果在调用reserve()函数之前已经包含了元素的话,调用reserve()函数中会重新分配内存并copy已存的元素,所以会发生iterator失效的问题。所以最好是在没有任何元素的前提下reserve()函数。在reserve()函数后,size()不变,capacity()==n。 来源: https://www.cnblogs.com/suexue/archive

Iterate over STL container using indices safe way to avoid using locks?

空扰寡人 提交于 2020-01-05 05:26:07
问题 Wondering if it's safe to iterate over a STL container such as a vector in the following manner to avoid locking on reads/writes but allowing for push_back() operations only by any "writing" threads. for (size_t i = 0; i < vec.size(); i++) { const T& t = *vec[i]; // do something with t } I understand that iterators can be invalidated by changes to the container but perhaps if we make sure the initial container size is large enough for any future additions, it should also be safe to iterate