[Solved] error: ‘class std::vector’ has no member named ‘sort’

There is no std::vector<…>::sort. Instead, you will need to use std::sort: std::sort(orderedList.begin(), orderedList.end()); However, this will try to compare the pointers in your std::vector<Shape*> (hint: refactor this if at all possible). Instead, you will need to pass a custom comparator that dereferences the pointers: std::sort( orderedList.begin(), orderedList.end(), [](Shape *a, Shape *b) { return *a < … Read more

[Solved] initialize 2d vector in c++

When you write adj[0], you’re implicitly assuming that the vector has a size of at least 1, in order for the zeroth element to exist. This is not the case for an empty vector, such as a newly initialized one. Unfortunately, this does not guarantee an error of any kind, it is Undefined Behavior, and … Read more

[Solved] Vectors using C++

why here vector <int> :: iterator i; The vector <int> :: iterator i; is created in order to traverse the vector vector <int> g1; (actually it can traverse any vector<int>, but in this case, that’s for g1) is there a difference between vector <int> :: iterator i; and int i? The scope of vector <int> … Read more

[Solved] C++ struct in a vector in an object(class)

The problem is that you define a type (account) in the class. account is a type so you should not declare it in the class : struct account { std::string name; float money; short pin; }; and then, the class becomes : class CBank { public: CBank(); account acc; std::vector<account> add; }; and the main … Read more

[Solved] Core dumped while using vector

Instead of printing result[0],result[1],result[2],first check the size of vector result and if it is ‘0’ then return 0 or whatever is given and otherwise return result vector. solved Core dumped while using vector

[Solved] How to access vector D[20]?

Well you are declaring an array of 20 vectors. So right way would be D[1].push_back(make_pair(0,make_pair(1,2)); int a = D[1][0].first; pair<int,int> b = D[1][0].second; b.second++; // You now have a pair containing increased value. Original remains unchanged. int z = D[1][0].second.second; // contains the nested pair’s second value If you want to increase the second – … Read more

[Solved] Ignoring the Last String in Vectors of Vectors for set_difference

Assumptions: using Lines = std::vector<std::vector<std::string>>; Lines current = { … }; // sorted on std::less<std::vector<std::string>> Lines input = { … }; // also sorted on std::less<std::vector<std::string>> Lines difference; Rather than doing std::set_difference(current.begin(), current.end(), input.begin(), input.end(), std::back_inserter(difference)); You should do auto compare = [](Lines::const_reference lhs, Lines::const_reference rhs) { assert(lhs.size() && rhs.size()) // same as default comparison, … Read more

[Solved] Vectors And Merging

I think your problem is that these lines: if (que1.empty()){ for (int m = j; m < counterq2; m++){ que_merge.push_back(que2.at(m)); } } else { for (int l = i; l < counterq1; ++l) { que_merge.push_back(que1.at(l)); } } doesn’t do what you expect. As far as I can see, your idea is to merge the remaining … Read more

[Solved] Creating a tic tac toe game. I want to create a 3×3 vector that stores the values for rows and columns then print out the board in the function

//Play Tic Tac Toe game between user and computer #include<iostream> #include<cstdio> #include<stdlib.h> #include<time.h> #define BLANK 5 using namespace std; /***************** Display the Matrix **********************************************/ //Display the matrix void display(int matrix[3][3]) { for(int i=0;i<3;i++){ for(int j=0;j<3;j++) cout<<matrix[i][j]<<“\t”; cout<<endl; } } /************** Chance of WIN Function *****************************************************/ //Funtion to detect the chance of either for user or … Read more

[Solved] A Codility test that needs to be solved

The function can look as it is shown in the demonstrative program #include <iostream> #include <algorithm> #include <vector> long long int solution( const std::vector<int> &v ) { long long int max_sum = 0; for ( auto it = v.begin(); ( it = std::find_if( it, v.end(), []( int x ) { return !( x < 0 … Read more