[Solved] Difference between vector and Derived vector class

In the first case, Derived is a class which can be used to declare a variable. In the second Derived is a variable name of type std::vector<Base>. In the case of the class it is possible to produce undefined behaviour with the following code: void deleter(std::vector<Base>* ptr) { delete ptr; } void buggy() { auto … Read more

[Solved] Convert Matrix to Vector [closed]

Your problem is a very specific one. I don’t see how this will be of any use to anybody but yourself. There is no ‘one line solution’. There are many ways to approach indexing problems, I like to use scalar indexing when possible: Ncolumns = size(Matrix,1); Nblocks = floor(Ncolumns/4); %number of 4-line blocks (excluding the … Read more

[Solved] 3-3. Write a program to count how many times each distinct word appears in its input

If you can’t use std::map, you can associate the word with the frequency: struct Info { std::string word; int frequency; }; //… std::vector<Info> database; //… std::string word; while (std::cin >> word) { // Find the word: const size_t length = database.size(); for (unsigned int i = 0; i < length; ++i) { if (database[i].word == … Read more

[Solved] How to access an index in a vector without segfaulting

After std::vector<float> some_vec; your vector is empty. You must not access any element in it then, because there isn’t any. If you want to put values into it, you need to append them to the vector using push_back() for (some iterator loop here) { //snip some_vec.push_back(some_float); i++; } Alternatively, if you know the size in … Read more

[Solved] C++ vector of strings segfault

As panta-rei correctly pointed out, it looks like you’re trying to contain a string of the form “string” + string form of (i) but you’re actually doing pointer arithmetic which is illogical in this case (you’re just passing a pointer incremented i from some location – who knows what’s in that memory?). In order to … Read more

[Solved] I need an algorithm to concatenate 2 vectors and also update the vector in cases of common element in C++ (not specific to C++11) [closed]

The most simple approach would be to just check for every element in src vector, if an duplicate exists in dest vector or not. If it exists just append Duplicate to it, else you can just push back the element as a new element in dest vector. The follow code would do the job – … Read more