vector

Comparator for vector<pair<int,int>> [duplicate]

泄露秘密 提交于 2021-02-08 12:12:33
问题 This question already has answers here : How do I sort a vector of pairs based on the second element of the pair? (7 answers) Closed 6 years ago . vector<pair<int,int> > v; for(i=0;i<5;i++){ cin>>b>>c; v.push_back(make_pair(b,c)); } sort(v.begin(),v.end()); Is it possible to write a comparator for the sort function such that v[i].first is sorted in increasing order and for similar values of v[i].first , v[i].second is sorted in decreasing order? like:- i/p: 13 10 44 15 13 15 13 99 6 45 o/p: 6

Vectors in C++ , bucket sort : Segmentation fault

﹥>﹥吖頭↗ 提交于 2021-02-08 12:07:28
问题 In my code, I am trying to implement bucket sort and in my implementation I have tried using vectors, but unfortunately I end up with errors with respect to vector functions. Code : #include <iostream> #include <algorithm> #include <vector> using namespace std; void bucket_sort(vector<float> & , int); //vector is passed by reference int main(){ vector<float> array; float element; int count; cout << "\nEnter the size of the vector : "; cin >> count; cout << "\nEnter the elements into the

Vectors in C++ , bucket sort : Segmentation fault

放肆的年华 提交于 2021-02-08 12:06:15
问题 In my code, I am trying to implement bucket sort and in my implementation I have tried using vectors, but unfortunately I end up with errors with respect to vector functions. Code : #include <iostream> #include <algorithm> #include <vector> using namespace std; void bucket_sort(vector<float> & , int); //vector is passed by reference int main(){ vector<float> array; float element; int count; cout << "\nEnter the size of the vector : "; cin >> count; cout << "\nEnter the elements into the

Loading output of System (char* command) to variable, c++

馋奶兔 提交于 2021-02-08 12:01:19
问题 ...Am trying to load/capture the output of system(char* command) function to a variable, a vector. can i have any possible way to push the output to my vector? I don*t want to write the output to file and read it again. Sample code: #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <fstream> #include <iostream> #include <string> #include <cstring> #include <sstream> #include <vector> using namespace std; int main() { vector <string> dir; system("pwd");//here i used this to

Strange output from dereferencing pointers into a vector

爷,独闯天下 提交于 2021-02-08 11:59:27
问题 I was writing a program in C++ where I need to have a 2d grid of pointers which point to objects which are stored in a vector. I tested out some part of the program and saw strange results in the output. I changed the objects to integers and removed everything non-essential to cut it down to the code snippet below, but I still get a weird output. vector<vector<int*>> lattice(10, vector<int*>(10));//grid of pointers vector<int> relevant;//vector carrying actual values onto which pointers will

Nested iterator class, begin() and end() sentinel issues

不问归期 提交于 2021-02-08 11:02:11
问题 I am having issues with my nested iterator class. The end() sentinel is returning 0, and I am very lost! Any help would be appreciated. I want this program to be able to take any type of input, e.g. an int, create the vector, and be able to fill the array until the end() sentinel has been reached, such as typing Q, or any non-integer. I took out a few lines of unnecessary code to shorten it. template <class T> class Set { private: vector<T> m_element; int size; public: Set() : size(0) {}; ...

Test whether position x is between any start (i=1 to i=max) and end (i=1 to i=max) positions stored in lists

独自空忆成欢 提交于 2021-02-08 09:10:17
问题 I have a simple data frame with specifies start and end positions within lists. These start and end positions define i number of regions. Now I would like to test whether a given position lies within such a region and if yes I need to know in which region (i). Here is a simple example data frame: start <- list(c(5,10,15), c(5) ,c(6,11),c(6,11)) end <- list(c(7,11,17), c(10), c(8,12),c(8,12)) imax <- c(3,1,2,2) position <- c(11,6,9,8) example <- data.frame(start = I(start), end = I(end), imax

Test whether position x is between any start (i=1 to i=max) and end (i=1 to i=max) positions stored in lists

眉间皱痕 提交于 2021-02-08 09:09:48
问题 I have a simple data frame with specifies start and end positions within lists. These start and end positions define i number of regions. Now I would like to test whether a given position lies within such a region and if yes I need to know in which region (i). Here is a simple example data frame: start <- list(c(5,10,15), c(5) ,c(6,11),c(6,11)) end <- list(c(7,11,17), c(10), c(8,12),c(8,12)) imax <- c(3,1,2,2) position <- c(11,6,9,8) example <- data.frame(start = I(start), end = I(end), imax

Mutable, random-access array/vector with high performance in haskell

 ̄綄美尐妖づ 提交于 2021-02-08 02:00:40
问题 This a topic on Haskell discussed a lot (e.g. mutable-array-implementation), but I am still not sure what is the best practice for the case requiring frequent modification and random-access of an array/vector. Say a vector of length 1,000,000. Operation on it involves accessing a (small, e.g 1000) subset of it based on input, and modifying the values based on the input. Furthermore, such operation are repeated 2,000,000 times. The task itself can be implemented in pure data structure such as

Writing/reading large vectors of data to binary file in c++

痴心易碎 提交于 2021-02-07 20:52:22
问题 I have a c++ program that computes populations within a given radius by reading gridded population data from an ascii file into a large 8640x3432-element vector of doubles. Reading the ascii data into the vector takes ~30 seconds (looping over each column and each row), while the rest of the program only takes a few seconds. I was asked to speed up this process by writing the population data to a binary file, which would supposedly read in faster. The ascii data file has a few header rows