containers

Copy std::vector into std::array

妖精的绣舞 提交于 2019-12-18 18:39:45
问题 How do I copy or move the first n elements of a std::vector<T> into a C++11 std::array<T, n> ? 回答1: Use std::copy_n std::array<T, N> arr; std::copy_n(vec.begin(), N, arr.begin()); Edit: I didn't notice that you'd asked about moving the elements as well. To move, wrap the source iterator in std::move_iterator. std::copy_n(std::make_move_iterator(v.begin()), N, arr.begin()); 回答2: You can use std::copy: int n = 2; std::vector<int> x {1, 2, 3}; std::array<int, 2> y; std::copy(x.begin(), x.begin()

Google Apps Script - How To Have One Script In multiple containers?

懵懂的女人 提交于 2019-12-18 15:09:12
问题 I have a container-bound Google Apps Script that is bound to a document, it works fine, adds the menu, works fine. My question is, how do I automatically have the container bound script automatically run in EVERY DOCUMENT? Also, Is it possible to make a chrome extension to modify google docs and if so how do I go about doing so? here is my code: var BLOCKS = "abcdefg"; var CLASSES = ["English", "History", "Science", "Writing", "Latin", "Math", "Study Skills"]; var FUNCTION_NAMES; var global =

Docker Rails app fails to be served - curl: (56) Recv failure: Connection reset by peer

╄→гoц情女王★ 提交于 2019-12-18 12:51:38
问题 I build a Rails app container with the following Dockerfile: $ cat Dockerfile FROM ruby:2.2 MAINTAINER Luca G. Soave <luca.soave@gmail.com> RUN apt-get update && apt-get install -y nodejs --no-install-recommends && rm -rf /var/lib/apt/lists/* RUN apt-get update && apt-get install -y mysql-client postgresql-client sqlite3 --no-install-recommends && rm -rf /var/lib/apt/lists/* RUN mkdir -p /usr/src/app WORKDIR /usr/src/app COPY . /usr/src/app/ RUN bundle install EXPOSE 3000 CMD ["rails",

How do stl containers get deleted?

[亡魂溺海] 提交于 2019-12-18 12:36:46
问题 How does container object like vector in stl get destroyed even though they are created in heap? EDIT If the container holds pointers then how to destroy those pointer objects 回答1: An STL container of pointer will NOT clean up the data pointed at. It will only clean up the space holding the pointer. If you want the vector to clean up pointer data you need to use some kind of smart pointer implementation: { std::vector<SomeClass*> v1; v1.push_back(new SomeClass()); std::vector<boost::shared

c# stack queue combination

删除回忆录丶 提交于 2019-12-18 12:09:44
问题 is there in C# some already defined generic container which can be used as Stack and as Queue at the same time? I just want to be able to append elements either to the end, or to the front of the queue thanks 回答1: Check the LinkedList class. LinkedList<int> list = new LinkedList<int>(); list.AddFirst(1); list.AddLast(2); list.AddFirst(0); 回答2: Here's my implementation of an immutable deque : http://blogs.msdn.com/ericlippert/archive/2008/02/12/immutability-in-c-part-eleven-a-working-double

How to iterate over a container in a thread-safe way?

痴心易碎 提交于 2019-12-18 11:56:11
问题 I have a container (C++) on which I need to operate in two ways, from different threads: 1) Add and remove elements, and 2) iterate through its members. Clearly, remove element while iteration is happening = disaster. The code looks something like this: class A { public: ... void AddItem(const T& item, int index) { /*Put item into my_stuff at index*/ } void RemoveItem(const T& item) { /*Take item out of m_stuff*/ } const list<T>& MyStuff() { return my_stuff; } //*Hate* this, but see class C

Partial match for the key of a std::map

匆匆过客 提交于 2019-12-18 11:28:09
问题 I have an std::map and I want to search for a key using a substring. For example, I have the following code: #include <iostream> #include <map> #include <string> using namespace std; typedef std::map<std::string, std::string> TStrStrMap; typedef std::pair<std::string, std::string> TStrStrPair; int main(int argc, char *argv[]) { TStrStrMap tMap; tMap.insert(TStrStrPair("John", "AA")); tMap.insert(TStrStrPair("Mary", "BBB")); tMap.insert(TStrStrPair("Mother", "A")); tMap.insert(TStrStrPair(

What is the purpose of std::scoped_allocator_adaptor?

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-18 10:41:57
问题 In the C++11 standard we have std::scoped_allocator_adaptor in the dynamic memory management library. What are the most important use cases of this class? 回答1: If you want a container of strings and want to use the same allocator for the container and its elements (so they are all allocated in the same arena, as TemplateRex describes) then you can do that manually: template<typename T> using Allocator = SomeFancyAllocator<T>; using String = std::basic_string<char, std::char_traits<char>,

Returning a pointer to a vector element in c++

别等时光非礼了梦想. 提交于 2019-12-18 10:07:38
问题 I have a vector of myObjects in global scope. I have a method which uses a std::vector<myObject>::const_iterator to traverse the vector, and doing some comparisons to find a specific element. Once I have found the required element, I want to be able to return a pointer to it (the vector exists in global scope). If I return &iterator , am I returning the address of the iterator or the address of what the iterator is pointing to? Do I need to cast the const_iterator back to a myObject, then

cast list<A*> to list<B*> where B inherits A

不羁的心 提交于 2019-12-18 08:49:15
问题 I got a function void doSomething(list<A*> list1, list<A*> list2) And classes class B : A class C : A Is there a direct way to call my function like void doSomething(list<B*> listOfB, list<C*> listOfC) or do I have to wrap it manually like void doSomething(list<B*> listOfB, list<C*> listOfC) { list<A*> l1; list<A*> l2; for (B* b : listOfB) l1.insert(b); for (C* c : listOfC) l2.insert(c); doSomething(l1, l2); //calling the function taking supertype } I tried unsuccessfully to cast list<B*> to