stdvector

vector::at vs. vector::operator[]

蓝咒 提交于 2020-01-26 21:56:36
问题 I know that at() is slower than [] because of its boundary checking, which is also discussed in similar questions like C++ Vector at/[] operator speed or ::std::vector::at() vs operator[] << surprising results!! 5 to 10 times slower/faster!. I just don't understand what the at() method is good for. If I have a simple vector like this one: std::vector<int> v(10); and I decide to access its elements by using at() instead of [] in situation when I have a index i and I'm not sure if its in

vector::at vs. vector::operator[]

妖精的绣舞 提交于 2020-01-26 21:54:20
问题 I know that at() is slower than [] because of its boundary checking, which is also discussed in similar questions like C++ Vector at/[] operator speed or ::std::vector::at() vs operator[] << surprising results!! 5 to 10 times slower/faster!. I just don't understand what the at() method is good for. If I have a simple vector like this one: std::vector<int> v(10); and I decide to access its elements by using at() instead of [] in situation when I have a index i and I'm not sure if its in

Should (in C++11) std::vector::resize(size_type) work for the default constructible value_type int[4]?

本小妞迷上赌 提交于 2020-01-21 10:52:35
问题 In C++11, there are two versions of std::vector::resize() : void resize( size_type count ); void resize( size_type count, const value_type& value); I understand (as suggested by one of the comments to one of the answers to this question) that the first requires value_type to be default constructible, while the second requires it to be copy constructible. However, (gcc 4.7.0) using namespace std; typedef int block[4]; vector<block> A; static_assert(is_default_constructible<block>::value,";-(")

error: no matching function for call to ‘std::vector<std::vector<int>>::push_back(std::vector<std::__cxx11::basic_string<char> >&)’

二次信任 提交于 2020-01-16 16:20:11
问题 I am trying to debug this code from my supervisor and I'm new to C++. I found a few similar no matching function for call to questions, which gave me ideas what the problem might be but was not able to solve it. I have listed my thoughts below the error message and relevant function. Error message: In function ‘int main(int, char**)’: distanceMatrixToSageGraph.c:104:43: error: no matching function for call to ‘std::vector<std::vector<int>>::push_back(std::vector<std::__cxx11::basic_string

Expand std::vector into parameter pack

北城以北 提交于 2020-01-16 01:15:29
问题 I have methods with the following signature: void DoStuff(int i); void DoStuff(int i, k); void DoStuff(int i, int k, int l); I have a method from where I would like to call the DoStuff methods as follows: void CallDoStuff(const std::vector<int>& vElements) { // What magic is supposed to happen here to make vElements an expandable pack? DoStuff(vElemets...); } Is there any chance to achieve this? Is using std::index_sequence the right way? If yes, could you please provide me a simple example

What is the difference between the vector operator [] and at()

落爺英雄遲暮 提交于 2020-01-15 09:48:47
问题 I'm messing around with a pointer to a vector of pointers std::vector<int*>* MyVector; Which I try to access using these 2 methods: MyVector->at(i); //This works MyVector[i] //This says "Expression must be a pointer to a complete object type" To my understanding, the difference between a vectors [] operator and at method is that the at method does additional boundary checks, so my question is why does the at method succeed in accessing the element whereas the [] operator does not? EDIT: Whole

Function Template Overloading or Specialization for inner template type std::vector<std::vector<T>>

纵然是瞬间 提交于 2020-01-14 14:01:29
问题 How to achieve function Template Overloading for inner template type std::vector<std::vector<T>> . I have a program of overloaded templates and a complex data structure having maps, pairs and vectors. #include <iostream> #include <vector> #include <map> #include <utility> #include <typeinfo> template<typename Test, template<typename...> class Ref> //#6 struct is_specialization : std::false_type {}; template<template<typename...> class Ref, typename... Args> //#7 struct is_specialization<Ref

shared_ptr<T> to shared_ptr<T const> and vector<T> to vector<T const>

♀尐吖头ヾ 提交于 2020-01-14 07:28:05
问题 I'm trying to define a good design for my software which implies being careful about read/write access to some variables. Here I simplified the program for the discussion. Hopefully this will be also helpful to others. :-) Let's say we have a class X as follow: class X { int x; public: X(int y) : x(y) { } void print() const { std::cout << "X::" << x << std::endl; } void foo() { ++x; } }; Let's also say that in the future this class will be subclassed with X1, X2, ... which can reimplement

Is it guaranteed that std::vector default construction does not call new?

[亡魂溺海] 提交于 2020-01-14 07:06:09
问题 According to the reference a simple std::vector<T> vec; creates an emtpy container (default constructor). Does this guarantee that there is no dynamic memory allocation? Or may an implementation chose to reserve some memory? I known that, for this empty constructor, there is no construction of the type T since C++11. However, I wonder, if there is also a guarantee that nothing is allocated on heap. I.e. that the above line is just a few nullptr on stack/member. I tested it with vc140, where

std::vector segmentation fault with and without initialization

大城市里の小女人 提交于 2020-01-14 05:50:10
问题 I was doing some experimentation with vectors in C++. The code I'm using is the following #include <vector> #include <iostream> int main() { std::vector<float> aVector = std::vector<float>(10); // IMPORTANT LINE std::cout << "Vector size: " << aVector.size() << std::endl; aVector.clear(); aVector[0] = 2.2; std::cout << "Vector size: " << aVector.size() << std::endl; aVector.push_back(0.1); std::cout << "Vector size: " << aVector.size() << std::endl; aVector.clear(); std::cout << "Vector size: