stdvector

Getting different address every time

时光毁灭记忆、已成空白 提交于 2019-12-10 21:11:07
问题 In the following code, I get a different address every time for the first element of std::vector v . Why is it so? #include <memory> #include <iostream> #include <vector> int main() { std::vector<int> v; for (int i=0; i<10; ++i) { int b = i; v.push_back(b); std::cout << std::addressof(v[0]) << std::endl; } return 0; } Output: 0x603010 0x603030 0x603010 0x603010 0x603050 0x603050 0x603050 0x603050 0x603080 0x603080 回答1: Because new memory may have to be allocated for the data contained in the

Pretty-print a std::vector in C++ [duplicate]

旧城冷巷雨未停 提交于 2019-12-10 18:34:16
问题 This question already has answers here : Pretty-print C++ STL containers (9 answers) Closed 6 years ago . How can I pretty-print a std::vector ? For example, if I construct a std::vector<int>(6, 1) , what can I run it through to get output like {1 1 1 1 1 1} in C++? It needs to be generic as the size and value might change, so std::vector<int>(4, 0) would be {0 0 0 0} . 回答1: #include <vector> #include <algorithm> #include <iterator> template<typename T> std::ostream & operator<<(std::ostream

Struct size containing vector<T> different sizes between DLL and EXE

久未见 提交于 2019-12-10 18:32:31
问题 I have this situation where an EXE program imports a DLL for a single function call. It works by passing in a custom structure and returning a different custom structure. Up till now it's worked fine until I wanted one of the structs data members to be a vector < MyStruct > When I do a sizeof(vector< MyStruct >) in my program I get a size of 20 but when I do it from inside the DLL I get a size of 24. This size inconsistency is causing a ESP pointer error. Can anyone tell me why a Vector <

Calling method of child class on a vector of parent class

风格不统一 提交于 2019-12-10 18:21:22
问题 #include <iostream> #include <vector> using namespace std; class Parent { public: Parent(); void method(); }; class Child: public Parent { public: Child(); void method(); }; int main() { vector<Parent> v; v.push_back(Parent()); v.push_back(Child()); v[0].method(); v[1].method(); return 0; } Parent::Parent() {} void Parent::method() { cout << "Parent." << endl; } Child::Child() {} void Child::method() { cout << "Child." << endl; } Basically I'd expect that program to print Parent. Child. but

How to initialize std stack with std vector?

杀马特。学长 韩版系。学妹 提交于 2019-12-10 18:19:08
问题 I need to put an std::vector into an std::stack . Here is my method so far(I am building a card game) : void CardStack::initializeCardStack(std::vector<Card> & p_cardVector) { m_cardStack = std::stack<Card>(); //code that should initialize m_cardStack with p_cardVector } Note : I cannot change my method signature because it is a imposed by a teacher... Do I have to iterate over the whole vector ? What is the most efficient way to do this ? The documentation. I have tried Jens answer but it

returning a pointed to an object within a std::vector

会有一股神秘感。 提交于 2019-12-10 16:25:42
问题 I have a very basic question on returning a reference to an element of a vector . There is a vector vec that stores instances of class Foo . I want to access an element from this vector . ( don't want to use the vector index) . How should I code the method getFoo here? #include<vector> #include<stdio.h> #include<iostream> #include<math.h> using namespace std; class Foo { public: Foo(){}; ~Foo(){}; }; class B { public: vector<Foo> vec; Foo* getFoo(); B(){}; ~B(){}; }; Foo* B::getFoo(){ int i;

Is in C# List something like vector.reserve(n) in C++

痴心易碎 提交于 2019-12-10 16:01:09
问题 When adding a lot of elements in System.Collections.Generic.List<T> it is running slow because when nums increases capacity it must copy all elements. In C++ this is fixed with vector.reserve(n) . How can i do that in C#? 回答1: Use Capacity property: list.Capacity = n; or you can set initial capacity via the constructor: var list = new List<int>(n); 来源: https://stackoverflow.com/questions/31118111/is-in-c-sharp-list-something-like-vector-reserven-in-c

Why does emplace_back(“Hello”) call strlen?

*爱你&永不变心* 提交于 2019-12-10 12:53:07
问题 Justin's answer on another question made an observation that I find very interesting but can't quite explain. Consider the following code: std::vector<std::string> v; v.push_back("Hello, world!"); // Doesn't call strlen. v.emplace_back("Hello, world!"); // Calls strlen. If you look at the assembly, emplace_back generates a call to strlen, whereas push_back does not (tested in gcc 8.1 and clang 6.0 using -Ofast ). Why is this happening? Why can't emplace_back optimize out the strlen call here?

How to efficiently copy a std::string into a vector

北战南征 提交于 2019-12-10 10:55:33
问题 I have a string std::string s = "Stack Overflow"; That I need to copy into a vector. This is how I am doing it std::vector<char> v; v.reserve(s.length()+1); for(std::string::const_iterator it = s.begin(); it != s.end(); ++it) { v.push_back( *it ); } v.push_back( '\0' ); But I hear range operation are more efficient. So I am thinking something like this std::vector<char> v( s.begin(), s.end()); v.push_back('\0'); But is this better in this case? What about the potential re-allocation when

deleting element objects of a std vector using erase : a) memory handling and b) better way?

强颜欢笑 提交于 2019-12-10 10:39:30
问题 I have a vec_A that stores instances of class A as: vec_A.push_back(A()); I want to remove some elements in the vector at a later stage and have two questions: a) The element is deleted as: vec_A.erase(iterator) Is there any additional code I need to add to make sure that there is no memory leak? . b) Assume that condition if(num <5) is if num is among a specific numberList. Given this, is there a better way to delete the elements of a vector than what I am illustrating below? #include<vector