vector

How reserve in std::vector works + Accessing vector with []

无人久伴 提交于 2021-02-20 04:44:07
问题 Why vector[n] = val doesn't give segmentation fault or changes the vector data, right before reserving an empty vector. Check this example: #include <iostream> #include <vector> int main() { std::vector<int> temp; temp.reserve(8); temp[0] = 1; temp[3] = 3; //why no attribution??? temp[7] = 1; temp[8] = 3; //why no segmentation fault??? std::cout << temp.size(); for(auto&a: temp){ //because the attribution didn't work, no loop needed std::cout << a; } return 0; } Also, why the operator []

How do you read n bytes from a file and put them into a vector<uint8_t> using iterators?

半腔热情 提交于 2021-02-19 15:30:23
问题 Based on this this question: How to read a binary file into a vector of unsigned chars In the answer they have: std::vector<BYTE> readFile(const char* filename) { // open the file: std::basic_ifstream<BYTE> file(filename, std::ios::binary); // read the data: return std::vector<BYTE>((std::istreambuf_iterator<BYTE>(file)), std::istreambuf_iterator<BYTE>()); } Which reads the entire file into the vector. What I want to do is read (for example) 100 bytes at a time in the vector, then do stuff,

How do you read n bytes from a file and put them into a vector<uint8_t> using iterators?

喜你入骨 提交于 2021-02-19 15:25:07
问题 Based on this this question: How to read a binary file into a vector of unsigned chars In the answer they have: std::vector<BYTE> readFile(const char* filename) { // open the file: std::basic_ifstream<BYTE> file(filename, std::ios::binary); // read the data: return std::vector<BYTE>((std::istreambuf_iterator<BYTE>(file)), std::istreambuf_iterator<BYTE>()); } Which reads the entire file into the vector. What I want to do is read (for example) 100 bytes at a time in the vector, then do stuff,

How do you read n bytes from a file and put them into a vector<uint8_t> using iterators?

百般思念 提交于 2021-02-19 15:24:39
问题 Based on this this question: How to read a binary file into a vector of unsigned chars In the answer they have: std::vector<BYTE> readFile(const char* filename) { // open the file: std::basic_ifstream<BYTE> file(filename, std::ios::binary); // read the data: return std::vector<BYTE>((std::istreambuf_iterator<BYTE>(file)), std::istreambuf_iterator<BYTE>()); } Which reads the entire file into the vector. What I want to do is read (for example) 100 bytes at a time in the vector, then do stuff,

How to print both the index and value for every element in a Vec?

£可爱£侵袭症+ 提交于 2021-02-19 06:42:34
问题 I'm trying to complete the activity at the bottom of this page, where I need to print the index of each element as well as the value. I'm starting from the code use std::fmt; // Import the `fmt` module. // Define a structure named `List` containing a `Vec`. struct List(Vec<i32>); impl fmt::Display for List { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { // Extract the value using tuple indexing // and create a reference to `vec`. let vec = &self.0; write!(f, "[")?; // Iterate over

How to print both the index and value for every element in a Vec?

拜拜、爱过 提交于 2021-02-19 06:42:11
问题 I'm trying to complete the activity at the bottom of this page, where I need to print the index of each element as well as the value. I'm starting from the code use std::fmt; // Import the `fmt` module. // Define a structure named `List` containing a `Vec`. struct List(Vec<i32>); impl fmt::Display for List { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { // Extract the value using tuple indexing // and create a reference to `vec`. let vec = &self.0; write!(f, "[")?; // Iterate over

R dplyr. Filter a dataframe that contains a column of numeric vectors

丶灬走出姿态 提交于 2021-02-19 04:22:07
问题 I have a dataframe in which one column contains numeric vectors. I want to filter rows based on a condition involving that column. This is a simplified example. df <- data.frame(id = LETTERS[1:3], name=c("Alice", "Bob", "Carol")) mylist=list(c(1,2,3), c(4,5), c(1,3,4)) df$numvecs <- mylist df # id name numvecs # 1 A Alice 1, 2, 3 # 2 B Bob 4, 5 # 3 C Carol 1, 3, 4 I can use something like mapply e.g. mapply(function(x,y) x=="B" & 4 %in% y, df$id, df$numvecs) which correctly returns TRUE for

Where is the performance gain of the erase-remove idiom coming from

南笙酒味 提交于 2021-02-19 03:15:40
问题 I need to erase all elements from a vector which fulfill a certain criteria. My first approach would be to loop through the vector and call vector::erase on all elements which fulfill the criteria. As far as I understand, vector::erase has a bad performance for this use case, because it removes the item from the underlying array, and moves the rest of the vector forward by one element (or more if you erase a range of elements). When you remove multiple elements, the rear elements will be

Converting 2D vector to 2D array

梦想与她 提交于 2021-02-19 01:14:57
问题 It's been a while since I last visited arrays (I've been working with vectors recently) and I need to convert an 2D vector back into a 2D array because of a library I am using accepts the paramaters of type double array where the accessors of this array is foo[i][j] for example. Here is my code: double** setupHMM(vector<vector<double> > &vals, int N, int M) { double** temp; temp = new double[N][M]; for(unsigned i=0; (i < N); i++) { for(unsigned j=0; (j < M); j++) { temp[i][j] = vals[i][j]; }

Converting 2D vector to 2D array

大憨熊 提交于 2021-02-19 01:07:43
问题 It's been a while since I last visited arrays (I've been working with vectors recently) and I need to convert an 2D vector back into a 2D array because of a library I am using accepts the paramaters of type double array where the accessors of this array is foo[i][j] for example. Here is my code: double** setupHMM(vector<vector<double> > &vals, int N, int M) { double** temp; temp = new double[N][M]; for(unsigned i=0; (i < N); i++) { for(unsigned j=0; (j < M); j++) { temp[i][j] = vals[i][j]; }