stl

Why doesn't a call to std::map::operator[] compile for a value type without default constructor? [duplicate]

时光怂恿深爱的人放手 提交于 2021-02-05 12:30:45
问题 This question already has answers here : Default constructor of the structure for correct std::map behaviour (2 answers) Closed 6 months ago . Consider the following class MyStruct : struct MyStruct { int x; int y; MyStruct(int i, int j): x(i), y(j) { } }; Note that MyStruct doesn't have a default destructor. The assignment m["AAAA"] = MyStruct(1, 1) in the code below doesn't compile: int main(int, char**) { map<string, MyStruct> m; m["AAAA"] = MyStruct(1, 1); return 0; } Why I need default

How to remove a struct record from an STL list

爱⌒轻易说出口 提交于 2021-02-05 12:24:55
问题 I have a list of type struct and I want to remove a specific record from that list. What is the best way to do this? I cannot figure out how to do it with .remove struct dat { string s; int cnt; }; void StructList() { list<dat>::iterator itr; //-- create an iterator of type dat dat data1; //-- create a dat object list<dat> dList; //-- create a list of type dat itr = dList.begin(); //-- set the dList itereator to the begining of dList string temp; //-- temp string var for whatever data1.s =

How to remove a struct record from an STL list

▼魔方 西西 提交于 2021-02-05 12:23:06
问题 I have a list of type struct and I want to remove a specific record from that list. What is the best way to do this? I cannot figure out how to do it with .remove struct dat { string s; int cnt; }; void StructList() { list<dat>::iterator itr; //-- create an iterator of type dat dat data1; //-- create a dat object list<dat> dList; //-- create a list of type dat itr = dList.begin(); //-- set the dList itereator to the begining of dList string temp; //-- temp string var for whatever data1.s =

STL set_union and set_intersection on unsorted vectors

落爺英雄遲暮 提交于 2021-02-05 08:12:13
问题 Below is my code on set intersection and union test that I did. I don't understand why it the output is incorrect when I commented out the sort function. Why is the sort necessary? or am I missing anything here? What is the technical reason for making the programmer responsible to sort the inputs first? Is it for guaranteeing sorting is not done twice(if the vector was already sorted and the algorithm sorts it again...)? #include <iostream> #include <vector> #include <algorithm> using

STL set_union and set_intersection on unsorted vectors

无人久伴 提交于 2021-02-05 08:12:10
问题 Below is my code on set intersection and union test that I did. I don't understand why it the output is incorrect when I commented out the sort function. Why is the sort necessary? or am I missing anything here? What is the technical reason for making the programmer responsible to sort the inputs first? Is it for guaranteeing sorting is not done twice(if the vector was already sorted and the algorithm sorts it again...)? #include <iostream> #include <vector> #include <algorithm> using

Constant Time Swap Logic for Vectors in C++ STL

有些话、适合烂在心里 提交于 2021-02-05 06:47:26
问题 Why is the time complexity required for swapping contents of two C++ STL vectors independent of the size of the corresponding vectors? Reference: http://www.cplusplus.com/reference/vector/vector/swap/ 回答1: A typical vector implementation stores: The allocator A pointer to the first element A pointer to the past-the-end position, or equivalently, the size A pointer to the end of the memory block owned by the vector, or equivalently, the capacity swap() simply swaps the pointers, and, if

DLL exporting causing issues with unique pointers

走远了吗. 提交于 2021-02-05 06:25:05
问题 I've got two files: Header.h #pragma once #ifdef UNIQUEPTRISSUE_EXPORTS #define UNIQUEPTRISSUE_API __declspec(dllexport) #else #define UNIQUEPTRISSUE_API __declspec(dllimport) #endif UniquePtrIssue.cpp #include "stdafx.h" #include "Header.h" #include <memory> #include <vector> class UNIQUEPTRISSUE_API ClassA { }; class UNIQUEPTRISSUE_API ClassB { private: std::vector<std::unique_ptr<ClassA>> x; }; Compiling raises the following error: 1>d:\program files (x86)\microsoft visual studio\2017

What are the template parameters of std::priority_queue?

醉酒当歌 提交于 2021-02-04 19:56:48
问题 I was looking through some STL documentation. I see that the syntax for a priority queue which stores in a descending order is: std::priority_queue<int> q ; //gives 9 8 7 6 5 4 3 2 1 when pushed and obtained However, for storing in an ascending fashion, it's: std::priority_queue< int, std::vector<int>, std::greater<int> > q ; //gives 1 2 3 4 5 6 7 8 9 when pushed and obtained I want to know what are the specific uses of the extra template parameters in the second example. As in, what does std

What are the template parameters of std::priority_queue?

試著忘記壹切 提交于 2021-02-04 19:56:13
问题 I was looking through some STL documentation. I see that the syntax for a priority queue which stores in a descending order is: std::priority_queue<int> q ; //gives 9 8 7 6 5 4 3 2 1 when pushed and obtained However, for storing in an ascending fashion, it's: std::priority_queue< int, std::vector<int>, std::greater<int> > q ; //gives 1 2 3 4 5 6 7 8 9 when pushed and obtained I want to know what are the specific uses of the extra template parameters in the second example. As in, what does std

reserve() - data() trick on empty vector - is it correct?

无人久伴 提交于 2021-02-04 19:34:05
问题 As we know, std::vector when initialized like std::vector vect(n) or empty_vect.resize(n) not only allocates required amount of memory but also initializes it with default value (i.e. calls default constructor). This leads to unnecessary initialization especially if I have an array of integers and I'd like to fill it with some specific values that cannot be provided via any vector constructor. Capacity on the other hand allocates the memory in call like empty_vect.reserve(n) , but in this