[Solved] Why doesnt vector support size as member variable like length variable in a Java class, instead a function size()? [closed]

Why doesn’t std::vector or other containers have length as a member variable, instead of a function? Because it would be writable if it was a public member variable. Since it makes no sense to write to the size member of a vector, the designers made the size member private and provided a size() method to … Read more

[Solved] Map::insert does not work [closed]

See the reference: template< class Key, class T, class Compare = std::less<Key>, class Allocator = std::allocator<std::pair<const Key, T> > > class map; http://en.cppreference.com/w/cpp/container/map An std::map takes a comparator that tells if the first argument is less than the second. Not if they are equal. Otherwise it could not build a binary tree. You don’t need … Read more

[Solved] C++ How to distinguish Multimap with duplicate keys with value and and Unique keys with values in different map containers with O(n) traversal

C++ How to distinguish Multimap with duplicate keys with value and and Unique keys with values in different map containers with O(n) traversal solved C++ How to distinguish Multimap with duplicate keys with value and and Unique keys with values in different map containers with O(n) traversal

[Solved] Extracting string from text [closed]

Not the most elegant solution but this should work #include <iostream> #include <vector> #include <string> #include <sstream> using namespace std; vector<string> split(string str, char delimiter) { vector<string> internal; stringstream ss(str); string tok; while(getline(ss, tok, delimiter)) { internal.push_back(tok); } return internal; } int main(int argc, char **argv) { string str = “My name is bob.I am … Read more

[Solved] Cannot convert char to char

The error is on this line: key[j]=words[index]; key is a std::string key; Therefore, key[j] is a char. words is a std::vector<std::string> words; Therefore, words[index] is a std::string. You cannot assign a std::string to a char. C++ doesn’t work this way. Your code is equivalent to the following: char a; std::string b; a=b; It is not … Read more

[Solved] sort vector of struct element

std::vector<student_t> st; for(unsigned i = 0; i < 10; ++i) st.push_back(student_t()); std::sort(st.begin(), st.end(), &compare); You could also use this vector constructor instead of lines 1-2: std::vector<student_t> st (10 /*, student_t() */); Edit: If you want to enter 10 students with the keyboard you can write a function that constructs a student: struct student_t &enter_student() { … Read more

[Solved] Why sort() function causes compilation error when it is used for set of class objects

Two problems. First, you cannot reorder the elements of a set. Their ordering criteria is determined upon construction, it is a fundamental part of the object. This is necessary in order for it to achieve O(log n) lookups, insertions, and deletions, which is part of the promises of std::set. By default, it will use std::less<Edge>, … Read more

[Solved] segmentation fault for vector iterator

In this declaration: vector<int>::iterator rit_i,rit_j,initial = vec.end(); only initial is initialized with vec.end(). To make it do what I think you expect, you have to write vector<int>::iterator rit_i = vec.end(), rit_j = vec.end(), initial = vec.end(); or vector<int>::iterator rit_i,rit_j,initial; rit_i = rit_j = initial = vec.end(); or something to that effect. solved segmentation fault for … Read more

[Solved] Remove all duplicate characters from a string (STL)

Solution simple as always: void RemoveDuplicates (std::string& input) { std::string::iterator it = std::unique(input.begin(), input.end()); input.erase(it, input.end()); std::cout << “New input = “<< input << std::endl; } Another solution to return a new string: std::string RemoveDuplicates (const std::string& input) { std::string newT(input); std::string::iterator it = std::unique(newT.begin(), newT.end()); newT.erase(it, newT.end()); return newT; } If desired result is … Read more