## Find an entry of in a matrix of vector C++

I want find the element v[0][0]=1 v[0][1]=2 of the vector v with the use of lambda function and find of stl. #include <iostream> #include <algorithm> using namespace std; int main() { vector<vector<int>> v(3); v[0].r...
more »

## Using a predicate to sort according to size

I understood that a predicate takes argument from an input range of iterators and does operations as intended, but when we supply a binary predicate and a pair of iterators, how does it work? How are the elements of a container passed to the predicat...
more »

## Can STL or Boost help sort a map by value?

The word "sorting" in the title may be misleading here. I'm looking for this behaviour: { 1: 100, { 1: 1, 3: 10, => 3: 10, 5: 1000, 5: 100, 9: 1 } 9: 1000 } That is, reassign the...
more »

## Is there any function in C++ std::string that count total number of same starting characters of two strings or any best way to do

Eg. abcfgf abdef ans=2 {"ab" is a common starting character} ...
more »

## Can I trust vector::size after an exception is thrown?

I am trying to understand how exceptions affect an std::vector. More precisely, I want to check the size of the vector, when an out of memory exception is thrown. I mean something like this: std::vector<int> v; try { for(unsigned int i = ...
more »

## Modify sorted array or sort the array everytime?

Problem: Given an array of size N, print the sorted sub sets of size K with consecutive elements. N = 10, K = 4 8 4 7 5 1 10 3 9 2 6 Output: 4 5 7 8, 1 4 5 7, 1 5 7 10, 1 3 5 10, ... Approach 1: Sort all the sub sets and print. Complexity Analy...
more »

## insert into std::map using class method

I searched a lot for this issue, but didn't find anything, so sorry if it's duplicate. I have a problem with inserting into std::map using class method which returns *this. If i try to insert more values, only the first one is actually inserted. Let...
more »

## C++: how to strip a "set" by removing multiple elements?

In STL algorithm, there're several "set" operations like set_union, set_difference, set_intersection, set_symmetric_difference，but I wish to do between 2 vectors: vector<int> A={1,2,3,4}; vector<int> B={1,3}; I wish to get A-B so A-B...
more »

## std::sort Doesn't work for User-Defined Object with oveloaded < operator

I am writing a class to test the efficient of different sorting algorithms (for a college course), and of the algorithms I'm supposed to test is the STL sort. To measure efficiency, we've defined a class Integer that holds and integer value, and also...
more »

## Why there is no std::sort(vector<T>&)

When sorting a vector in C++ you can use STL as: std::sort(vec.begin(), vec.end()); As this is very common usage (i.e. providing begin and end iterator) I wonder why there is no sort overload which would accept a reference to the vector e.g: temp...
more »

## Why do we need to learn different Sorting algorithms when the STL sort function is already available to us in C++?

Yesterday, this question came in my mind. Although I have neither read all the sorting algorithms like Quicksort, Merge Sort, Heapsort Insertion Sort, Selection Sort, and Bubble Sort nor I have read the Introduction to Algorithms by CLRS but still, I...
more »

## STL sorting in nested data structure

this is STL based data structure that I created for representing graph in C++. typedef std::pair<int,int> ii; typedef std::vector<ii> vii; typedef std::vector< vii > graph; in Kruskal algorithm that I read in Steven Halim's (Comp...
more »

## Sorting list with extra arguments to comparison function

Given the following class Coordinate { int i,j; }; list<shared_ptr<Coordinate>> coordinates; (Coordinate is just a much simpler version of what my code really contains.) I want to sort the list with respect to minimal Euclidean dist...
more »

## Why does std::sort assume a std::vector< std::vector<int> > to be a std::vector by default, thereby creating wrong results?

I have created a vector of vectors and I would like to sort them based on parameters that I define. Here, the sort() function takes the variable dataset defined as vector<vector<int>> to be just a vector<int>. Could someone explain ...
more »

## C++ sort cannot sort set of strings?

I am wondering how I can sort a set that contains strings. For example, I have a set: std::set<std::string> setA = {"B","A","C"} Then I wanna use this to do the sorting: std::sort(setA.begin(),setA.end()); But the C++ compiler cannot let ...
more »

## C++/STL public iterators for deeply nested private data

Consider the below class definition featuring deeply nested private data. template <typename T, typename U, typename V> class NestedData { private: typedef std::vector<V> L3; typedef std::map<U, L3> L2; typedef std::map<...
more »

## Why doesn't std::function work in this situation?

Imagine I have a type: struct my_type { double operator()(int a) { return 3.1415; } }; Then I'd like to wrap it in std::function. Consider two different approaches: my_type m_t; std::function<double(int)> f(std::move(m_t...
more »

## Difference between f and &f

Consider the code: void f(int) {} int main() { std::function<void(int)>func = f; func(3); std::function<void(int)>funcc = &f; funcc(3); void(*ptr)(int) = f; ptr(3); void(*ptrr)(int) = &f; ptrr(3)...
more »

So I'm a C++ programmer student, and I'm starting to learn Java, and I would like to read a class called Candidate with two string values (Name and ID), and I would like to store these Candidates in a container. What would be the best container to u...
more »

## How to check if a container passed to a function is sorted and sort it if not

I have this function we wrote some time ago: template <class C, class T> static inline bool findClosestObject( const C& container, const TimeUnit& now, T& object ); C is a container of T elements TimeUnit is a class encapsulatin...
more »

## C++ offers std::sort, but does it also offer something to search efficiently?

I have a vector of objects which implement operator< and operator==. C++ offers std::sort to sort that vector efficiently. Is there also a function in std to efficiently search a vector repeatedly? I will have many searches on that sorted vect...
more »

## Sort a matrix defined as a vector<double>

Assume I have a square matrix A of size n, defined as a std::vector<double>. std::vector<double> A(n*n); The elements of the matrix are accessed the usual way: double a_ij = A[i*n + j]; I need to sort the rows of the matrix in ascen...
more »

## c++ std::map class with generic key

I have a family of classes, and each subclass needs a map but the keys will have different types, although they both will perform the exact same operations with the map. Also the value on both cases will be string. So far I have code similar to the e...
more »

## Three.js prevent seeing file in resources

When user click to specific 3Dmodel I get it from server render and display in browser using three.js. Some of models are free and some of them has a price. The problem comes when user click to model which is not free - and in fact he can go to conso...
more »