stdvector

Easiest way to make a cyclic iterator (circulator)?

China☆狼群 提交于 2019-11-26 12:25:27
问题 I have an object that I want to travel in a continuous loop in a game. I have a series of coordinates in a std::vector that I want to use as waypoints. Is there any way to make an std::vector<T>::iterator cyclic (also known as a circulator)? The best I can come up with is to have two iterators and then whenever the first iterator is exhausted assign to it the value of the second (which would not be used to do anything else) but I am not even sure it will work - will the assignment operator

Unresolved externals in C++ when using vectors and find

隐身守侯 提交于 2019-11-26 11:29:05
问题 I have tried this code in a totally separate project, and it works fine (the only difference being that the project that is not working is being exported as a DLL). Here is the code: RTATMATHLIB.CPP #include \"stdafx.h\" #include \"RTATMATHLIB.h\" #include <math.h> #include <vector> #include <algorithm> #include <stdexcept> using namespace std; double someFunc(double** Y, int length) { vector<double> myVector; for(int i = 0; i < length; i++) { double value = (*Y)[i]; vector<double>::iterator

vector push_back calling copy_constructor more than once?

房东的猫 提交于 2019-11-26 09:58:14
问题 I am a bit confused with the way vector push_back behaves, with the following snippet I expected the copy constructor to be invoked only twice, but the output suggest otherwise. Is it a vector internal restructuring that results in this behaviour. Output: Inside default Inside copy with my_int = 0 Inside copy with my_int = 0 Inside copy with my_int = 1 class Myint { private: int my_int; public: Myint() : my_int(0) { cout << \"Inside default \" << endl; } Myint(const Myint& x) : my_int(x.my

Why doesn&#39;t vector::clear remove elements from a vector?

久未见 提交于 2019-11-26 09:47:01
问题 When I use clear() on a std::vector , it is supposed to destroy all the elements in the vector , but instead it doesn\'t. Sample code: vector<double> temp1(4); cout << temp1.size() << std::endl; temp1.clear(); cout << temp1.size() << std::endl; temp1[2] = 343.5; // I should get segmentation fault here .... cout << \"Printing..... \" << temp1[2] << endl; cout << temp1.size() << std::endl; Now, I should have gotten segmentation fault while trying to access the cleared vector, but instead it

Correct way to work with vector of arrays

℡╲_俬逩灬. 提交于 2019-11-26 08:20:00
Could someone tell what is the correct way to work with a vector of arrays? I declared a vector of arrays ( vector<float[4]> ) but got error: conversion from 'int' to non-scalar type 'float [4]' requested when trying to resize it. What is going wrong? You cannot store arrays in a vector or any other container. The type of the elements to be stored in a container (called the container's value type ) must be both copy constructible and assignable. Arrays are neither. You can, however, use an array class template, like the one provided by Boost, TR1, and C++0x: std::vector<std::array<double, 4> >

Is std::vector copying the objects with a push_back?

狂风中的少年 提交于 2019-11-26 07:53:07
问题 After a lot of investigations with valgrind, I\'ve made the conclusion that std::vector makes a copy of an object you want to push_back. Is that really true ? A vector cannot keep a reference or a pointer of an object without a copy ?! Thanks 回答1: Yes, std::vector<T>::push_back() creates a copy of the argument and stores it in the vector. If you want to store pointers to objects in your vector, create a std::vector<whatever*> instead of std::vector<whatever> . However, you need to make sure

How do I sort a vector of pairs based on the second element of the pair?

那年仲夏 提交于 2019-11-26 03:24:22
问题 If I have a vector of pairs: std::vector<std::pair<int, int> > vec; Is there and easy way to sort the list in increasing order based on the second element of the pair? I know I can write a little function object that will do the work, but is there a way to use existing parts of the STL and std::less to do the work directly? EDIT: I understand that I can write a separate function or class to pass to the third argument to sort. The question is whether or not I can build it out of standard stuff

Vector going out of bounds without giving error

99封情书 提交于 2019-11-26 02:58:46
问题 I have a std::vector . I check its size which is 6 but when I try to access vec[6] to check whether it will give error, I get no error but some number instead. Should not it give an error? edit: something like: struct Element { std::vector<double> face; }; int main() { Element elm; .... // insert 6 elements into elm.face std::cout << elm.face.size() << std::endl; // answer is 6 std::cout << elm.face[6] << std::endl; // answer is some number } 回答1: STL vectors perform bounds checking when the

Correct way to work with vector of arrays

本秂侑毒 提交于 2019-11-26 01:50:05
问题 Could someone tell what is the correct way to work with a vector of arrays? I declared a vector of arrays ( vector<float[4]> ) but got error: conversion from \'int\' to non-scalar type \'float [4]\' requested when trying to resize it. What is going wrong? 回答1: You cannot store arrays in a vector or any other container. The type of the elements to be stored in a container (called the container's value type ) must be both copy constructible and assignable. Arrays are neither. You can, however,

How to shuffle a std::vector?

丶灬走出姿态 提交于 2019-11-26 00:54:48
问题 I am looking for a generic, reusable way to shuffle a std::vector in C++. This is how I currently do it, but I think it\'s not very efficient because it needs an intermediate array and it needs to know the item type (DeckCard in this example): srand(time(NULL)); cards_.clear(); while (temp.size() > 0) { int idx = rand() % temp.size(); DeckCard* card = temp[idx]; cards_.push_back(card); temp.erase(temp.begin() + idx); } 回答1: From C++11 onwards, you should prefer: #include <algorithm> #include