stdvector

What is the memory layout of vector of arrays?

天涯浪子 提交于 2020-06-24 18:14:43
问题 can anybody explaine the memory layout of std::vector<std::array<int, 5>> vec(2) does it provide contiguous memory block of a 2D array with 2 rows of 5 elements? To my understanding, the vector of vectors std::vector<std::vector<int>> vec(2, std::vector<int>(5)) provide the memory layout of two contiguous arrays of length 5 element s in different locations in memory. Will it be the same for the vector of arrays? 回答1: Arrays do not have any indirection, but just store their data "directly".

std::vector of functions

五迷三道 提交于 2020-06-10 08:36:39
问题 I want a std::vector to contain some functions, and that more functions can be added to it in realtime. All the functions will have a prototype like this: void name(SDL_Event *event); I know how to make an array of functions, but how do I make a std::vector of functions? I've tried this: std::vector<( *)( SDL_Event *)> functions; std::vector<( *f)( SDL_Event *)> functions; std::vector<void> functions; std::vector<void*> functions; But none of them worked. Please help 回答1: Try using a typedef:

std::vector of functions

回眸只為那壹抹淺笑 提交于 2020-06-10 08:36:06
问题 I want a std::vector to contain some functions, and that more functions can be added to it in realtime. All the functions will have a prototype like this: void name(SDL_Event *event); I know how to make an array of functions, but how do I make a std::vector of functions? I've tried this: std::vector<( *)( SDL_Event *)> functions; std::vector<( *f)( SDL_Event *)> functions; std::vector<void> functions; std::vector<void*> functions; But none of them worked. Please help 回答1: Try using a typedef:

std::vector vs std::stack

家住魔仙堡 提交于 2020-06-10 07:26:49
问题 What is the difference between std::vector and std::stack ? Obviously vectors can delete items within the collection (albeit much slower than list) whereas the stack is built to be a LIFO-only collection. However, are stacks faster for end-item manipulation? Is it a linked list or dynamically re-allocated array? I can't find much information about stacks, but if I'm picturing them correctly (they are similar to an actual thread stack; push, pop, etc. - along with that top() method) then they

How to get array size stored in unique_ptr?

牧云@^-^@ 提交于 2020-05-28 18:31:44
问题 if I do: std::unique_ptr<int[]> uparr(new int[1984]); and I pass uparr to somebody without passing the 1984 to them can they see how many elements it has? aka is there equivalent of vector's .size() for unique_ptr of array ? 回答1: No, there isn't. Dynamic arrays are a somewhat defective language feature. You'll essentially always want/need to pass the array length around separately (unless you have some kind of sentinel policy). So you might as well use std::vector (if you don't mind the extra

Is there a way to display all saved objects in vector classes?

一曲冷凌霜 提交于 2020-05-17 05:47:12
问题 I coded this for a bus system but having trouble displaying the objects that have been saved in stud1 . I tried using readData but didn't work. The purpose of the code is to 1. receive input(s) in the form of bus info from the user and save them and 2. output all buses input into the system(reposted altered code) #include <iostream> #include <fstream> #include <vector> using namespace std; string busType, busMake, regNum; char menu(); int id = 0; //int staff[50]; int carObjNum, option0; int

Weird behaviour with class fields when adding to a std::vector

浪尽此生 提交于 2020-05-09 20:06:41
问题 I have found some very weird behaviour (on clang and GCC) in the following situation. I have a vector, nodes , with one element, an instance of class Node . I then call a function on nodes[0] that adds a new Node to the vector. When the new Node is added, the calling object's fields are reset! However, they seem to return to normal again once the function has finished. I believe this is a minimal reproducible example: #include <iostream> #include <vector> using namespace std; struct Node;

taking over memory from std::vector

风流意气都作罢 提交于 2020-04-09 08:00:13
问题 I use an external library which operates on large quantities of data. The data is passed in by a raw pointer, plus the length. The library does not claim ownership of the pointer, but invokes a provided callback function (with the same two arguments) when it is done with the data. The data gets prepared conveniently by using std::vector<T> , and I'd rather not give up this convenience. Copying the data is completely out of the question. Thus, I need a way to "take over" the memory buffer

replacing the command line arguments int argc and char** argv with std::vector<std::string>

三世轮回 提交于 2020-03-14 18:37:48
问题 Following this post, where I have found a temporary workaround for my other problem, I want to know if I can replace the int argc, char** argv with a std::vector<std::string> variable/object. Consider the imaginary code: #include <iostream> #include <CloseLibrary> void someFunction(int argc, char** argv){ for (int i = 0; i < argc; ++i) { std::cout << argv[i] << std::endl; } } int myFunc(int argc, char** argv){ someFunction(argc, argv); return 0; } where the CloseLibrary is a closed library

replacing the command line arguments int argc and char** argv with std::vector<std::string>

我的梦境 提交于 2020-03-14 18:37:20
问题 Following this post, where I have found a temporary workaround for my other problem, I want to know if I can replace the int argc, char** argv with a std::vector<std::string> variable/object. Consider the imaginary code: #include <iostream> #include <CloseLibrary> void someFunction(int argc, char** argv){ for (int i = 0; i < argc; ++i) { std::cout << argv[i] << std::endl; } } int myFunc(int argc, char** argv){ someFunction(argc, argv); return 0; } where the CloseLibrary is a closed library