stl

When to use emplace* and when to use push/insert [duplicate]

浪子不回头ぞ 提交于 2019-12-23 02:56:36
问题 This question already has answers here : push_back vs emplace_back (7 answers) Closed 6 years ago . I know of general idea of emplace functions on containers("construct new element inplace"). My question is not what it does, but more of like Effective C++11 one. What are good rules for deciding when to use (for eg when it comes to std::vector ) emplace_back() and when to use push_back() and in general emplace* vs "old" insert functions? 回答1: emplace_back() only really makes sense when you

Memory is corrupted after pushing into the vector

ⅰ亾dé卋堺 提交于 2019-12-23 02:18:10
问题 Why the memory is being corrupted after pushing into the vector. In the below program I have a struct with a string var(it is not a pointer). I am creating a local struct object each time and assigning a string value and push to the vector. After pushing to the vector I am making changes in the local struct object. But this change is being reflected in vector struct object's string data. #include <iostream> #include <vector> #include <string> #include <memory.h> using namespace std; void

Why does the C++ STL not provide a set of thread-safe containers? [duplicate]

社会主义新天地 提交于 2019-12-23 02:01:51
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: question about STL thread-safe and STL debugging I'm currently being engaged in a project which is developed using C++. Recently we are considering replacing some self-defined thread-safe containers with some STL equivalents to gain some efficiency. However, after looking for a while, I found that there is no a thread-safe container provided in STL at all, which surprises quite a lot. Is there any reason? 回答1:

C++ STL stack pop operation giving segmentation fault

一曲冷凌霜 提交于 2019-12-23 01:50:05
问题 I implemented a programming question from this link in C++ but I am getting a segmentation fault in the pop() operation with my code. I am fairly new to C++ and can not seem to find the error myself. #include<iostream> #include<stack> using namespace std; void printNge(int *arr); int main() { int arr[] = {1,4,2,6,3,8,7,2,6}; printNge(arr); return 0; } void printNge(int *arr) { stack<int> st; st.push(arr[0]); for(int i=1; i<9;i++) { while((st.top() < arr[i]) && (!st.empty())) { cout <<

STL priority_queue

旧街凉风 提交于 2019-12-23 01:40:56
priority_queue   优先队列(Priority Queues) :顾名思义,一个有着 优先级 的队列。它是一种ADT,和队列的思想差不多—— 排队 ,数据结构中的队列是不能插队的,不能颠倒排队的顺序,而在优先队列里,先出队列的元素不是先进队列的元素,而是 优先级高 的元素,默认的优先级是数字大的数优先级高。当然用户是可 自定义类型 的,这样就必须为元素定义一个优先级。因为出队元素不是最先进队的元素,则出队的方法有queue的front() 变为top()。 priority_queue<Type, Container, Functional> Type为数据类型, Container为保存数据的容器,Functional为元素比较方式。 如果不写后两个参数,那么容器默认用的是vector,比较方式默认用operator<,也就是优先队列是大顶堆,队头元素最大。 定义 namespace std{ template <typename T, typename Container = vector<T>, typename Compare = less<typename Container::value_type> > class priority_queue; } 注意需要头文件: < queue > priority_queue<int>pq1; or

C++ STL 教程

烈酒焚心 提交于 2019-12-23 01:29:55
Table of Contents C++ STL 教程 实例 C++ STL 教程 在前面的章节中,我们已经学习了 C++ 模板的概念。C++ STL(标准模板库)是一套功能强大的 C++ 模板类,提供了通用的模板类和函数,这些模板类和函数可以实现多种流行和常用的算法和数据结构,如向量、链表、队列、栈。 C++ 标准模板库的核心包括以下三个组件: 组件 描述 容器(Containers) 容器是用来管理某一类对象的集合。C++ 提供了各种不同类型的容器,比如 deque、list、vector、map 等。 算法(Algorithms) 算法作用于容器。它们提供了执行各种操作的方式,包括对容器内容执行初始化、排序、搜索和转换等操作。 迭代器(iterators) 迭代器用于遍历对象集合的元素。这些集合可能是容器,也可能是容器的子集。 这三个组件都带有丰富的预定义函数,帮助我们通过简单的方式处理复杂的任务。 下面的程序演示了向量容器(一个 C++ 标准的模板),它与数组十分相似,唯一不同的是,向量在需要扩展大小的时候,会自动处理它自己的存储需求: 实例 #include <iostream> #include <vector> using namespace std; int main() { // 创建一个向量存储 int vector<int> vec; int i; //

Best way to erase vector of ranges from std::vector

◇◆丶佛笑我妖孽 提交于 2019-12-23 01:07:05
问题 In one of my projects it is necessary to remove certain elements from a std::vector<double> values . The indices I have to remove are given as a vector of intervals. For example {1,3} means, that I have to remove indices from 1 to 3 inclusive from values . I can assume that the intervals given are mutually exclusive. The code shown below illustrates, what the desired behavior should look like. #include <iostream> #include <vector> int main(int argc, char** args) { // Intervals of indices I

How to overwrite the default behavor of the construct method in the allocator class in C++ STL

你离开我真会死。 提交于 2019-12-22 18:41:44
问题 How do you overwrite the default behavior of the construct method in the allocator class in STL? The following does not seem to work: #include <list> #include <iostream> #include <memory> struct MyObj { MyObj() { std::cout << "This is the constructor" << std::endl; } MyObj(const MyObj& x) { std::cout << "This is the copy constructor" << std::endl; } }; class MyAlloc : public std::allocator <MyObj>{ public: void construct(pointer p, const_reference t){ std::cout << "Construct in the allocator"

c++, creating vector of vectors?

醉酒当歌 提交于 2019-12-22 18:29:17
问题 Question is about what is the best way to create vector of vector s. I have several vector<double> coordinates; and I want pass them to function. How I should combine them, vector<vector<double> > ? Is there more elegant way? 回答1: That sounds like a reasonable approach. If you're worried about readability, then use a typedef . However, if all of your vectors are the same length (e.g. you're really trying to create a 2D array), then consider using a boost::multi_array. 回答2: Just like you said

STL vector简介

荒凉一梦 提交于 2019-12-22 11:34:27
STL vector简介 vector(向量): C++中的一种数据结构,确切的说是一个类.它相当于一个动态的数组,当程序员无法知道自己需要的数组的规模多大时,用其来解决问题可以达到最大节约空间的目的. 用法: 1.文件包含: 首先在程序开头处加上#include<vector>以包含所需要的类文件vector 还有一定要加上using namespace std; 2.成员函数 1.push_back 在数组的最后添加一个数据 2.pop_back 去掉数组的最后一个数据 3.at 得到编号位置的数据 4.begin 得到数组头的指针 5.end 得到数组的最后一个单元+1的指针 6.front 得到数组头的引用 7.back 得到数组的最后一个单元的引用 8.max_size 得到vector最大可以是多大 9.capacity 当前vector分配的大小 10.size 当前使用数据的大小 11.resize 改变当前使用数据的大小,如果它比当前使用的大,者填充默认值 12.reserve 改变当前vecotr所分配空间的大小 13.erase 删除指针指向的数据项 14.clear 清空当前的vector 15.rbegin 将vector反转后的开始指针返回(其实就是原来的end-1) 16.rend 将vector反转构的结束指针返回(其实就是原来的begin-1)