stl-algorithm

Overloading comparison operators to work with STL sort in C++

混江龙づ霸主 提交于 2019-12-08 02:39:40
问题 I am writing a program that will read in a list of names with social security numbers (not real ones of course) and sort the list based on either last name or ssn, depending on a command line argument. I have overloaded the < operator and also overloaded input and output operators for simplicity. Everything compiles fine until I add the sort function and the output at the end of main. I'm stumped. Any ideas? Any other tips are also greatly appreciated. #include <algorithm> #include <iostream>

LCS ALGORITHM ( example )

荒凉一梦 提交于 2019-12-07 15:13:34
There's a dynamic programming algorithm to find the Longest Common Subsequence of two sequences. How can I find the LCS algorithm of two sequences X and Y. (Test of correctness) (a) X = ABEDFEESTYH Y=ABEDFEESTYHABCDF (b) X = BFAAAABBBBBJPRSTY Y=ABCDEFGHIJKLMNOPRS (c) X = ϕ (Empty Sequence), Y = BABADCAB Here is an online calculator http://igm.univ-mlv.fr/~lecroq/seqcomp/node4.html Java public class LCS { public static void main(String[] args) { String x = StdIn.readString(); String y = StdIn.readString(); int M = x.length(); int N = y.length(); // opt[i][j] = length of LCS of x[i..M] and y[j.

“vector iterator not incrementable” run-time error with set_intersection

允我心安 提交于 2019-12-07 14:33:42
问题 Why does this code result in a run-time error "vector iterator not incrementable"? vector<string> s1, s2; s1.push_back("joe"); s1.push_back("steve"); s1.push_back("jill"); s1.push_back("svetlana"); s2.push_back("bob"); s2.push_back("james"); s2.push_back("jill"); s2.push_back("barbara"); s2.push_back("steve"); sort(s1.begin(), s1.end()); sort(s2.begin(), s2.end()); vector<string> result; vector<string>::iterator it_end, it_begin; it_end = set_intersection(s1.begin(), s1.end(), s2.begin(), s2

how to iterate into a smaller container (i.e. stride != 1)

故事扮演 提交于 2019-12-07 12:57:24
问题 There is a question that is very similar in spirit here. Unfortunately that question didn't prompt much response - I thought I would ask a more specific question with the hope that an alternative method can be suggested. I'm writing a binary file into std::cin (with tar --to-command=./myprog ). The binary file happens to be a set of floats and I want to put the data into std::vector<float> - ideally the c++ way. I can generate a std::vector<char> very nicely (thanks to this answer) #include

range v3 flattening a sequence

我只是一个虾纸丫 提交于 2019-12-07 09:51:23
问题 So I recently watched this talk on c++: https://www.youtube.com/watch?v=mFUXNMfaciE And I was very interested on trying it out. So after some toy program I am stuck on how to properly flatten a vector of vectors into a vector. According to the documentation here: https://ericniebler.github.io/range-v3/ This is possible using ranges::view::for_each . However I just can't seem to get it to work. Here is some minimal code. #include <range/v3/all.hpp> #include <iostream> #include <vector> int

std::copy from a range into itself

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-06 08:25:54
This might be a silly question and the answer may simply be "Because," but I'm curious so here it goes. Suppose I want to copy the final elements of an std::vector to the front without regard to what happens elsewhere. I can do this with a simple function call #include <vector> #include <algorithm> std::vector<int> v; . . . std::copy(v.begin() + N, v.end(), v.begin()); What I find surprising is that the standard seems to brand this undefined behavior if N == 0 (unless v.empty() , I suppose). Since even a naive implementation of std::copy won't have problems in that case (and in fact yield a

“vector iterator not incrementable” run-time error with set_intersection

删除回忆录丶 提交于 2019-12-05 19:51:12
Why does this code result in a run-time error "vector iterator not incrementable"? vector<string> s1, s2; s1.push_back("joe"); s1.push_back("steve"); s1.push_back("jill"); s1.push_back("svetlana"); s2.push_back("bob"); s2.push_back("james"); s2.push_back("jill"); s2.push_back("barbara"); s2.push_back("steve"); sort(s1.begin(), s1.end()); sort(s2.begin(), s2.end()); vector<string> result; vector<string>::iterator it_end, it_begin; it_end = set_intersection(s1.begin(), s1.end(), s2.begin(), s2.end(), result.begin()); cout << int (it_end - result.begin()) << endl; for_each(result.begin(), result

how to iterate into a smaller container (i.e. stride != 1)

早过忘川 提交于 2019-12-05 18:58:20
There is a question that is very similar in spirit here . Unfortunately that question didn't prompt much response - I thought I would ask a more specific question with the hope that an alternative method can be suggested. I'm writing a binary file into std::cin (with tar --to-command=./myprog ). The binary file happens to be a set of floats and I want to put the data into std::vector<float> - ideally the c++ way. I can generate a std::vector<char> very nicely (thanks to this answer ) #include <fstream> #include <iostream> #include <iterator> #include <algorithm> #include <vector> int main (int

std::is_sorted and strictly less comparison?

六眼飞鱼酱① 提交于 2019-12-05 00:41:00
I do not understand well the std::is_sorted algorithm and its default behaviour. If we look to cppreference , it says that by default std::is_sorted uses the < operator. Instead of that, I find that using <= would be natural. But my problem is that for the following list of numbers : 1 2 3 3 4 5 it will return true , even if 3 < 3 should be false . How is that possible ? EDIT: its seems to be worse than what I thought, because passing std::less_equal<int> will return false in that case... What is the condition applied when I pass a comparator function? Per 25.4/5: A sequence is sorted with

Find First Two Non-Adjacent Elements in a set Using an STL Algorithm

倖福魔咒の 提交于 2019-12-04 05:59:22
问题 So I really struggled with this and even now I am not happy with my solution. I have a set that at least contains 0, and may contain other positive int s. I need to find the first positive number not in the set . So writing a standard while -loop to accomplish this is easy. i = foo.begin(); while (i != prev(foo.end()) && *i + 1 == *next(i)){ ++i; } cout << "First number that cannot be formed " << *i + 1 << endl; But when I try to write an STL algorithm version of the loop I get something that