1

newbie here. I have two questions.

Firstly, I tried Initializing my vector in this format:

vector<string> v = {"plan", "man", "canal"}; 

I get this error: in C++98 ‘v’ must be initialized by constructor, not by ‘{...}’

what does it mean by a constructor? Is it the same with the way I initialized the vector<disliked> used in the code below?

#include "library/std_lib_facilities.h"

int main() 
{
    vector<string> disliked;
    disliked.push_back("gin");
    disliked.push_back("beer");
    vector<string> words; 
    for(string word; cin >> word;) 
        words.push_back(word);
    for(int i = 0; i < words.size(); ++i) {
        bool match = false; 
        for(int x = 0; x < disliked.size(); ++x) {
            if(disliked[x] == words[i])
                match = true;
        }
        if(match) 
            cout << "BLEEP!!" << '\n';
        else
            cout << words[i] << '\n';
    }
}

Secondly, the code works as such: It takes input and stores them in the vector<words> when the input is terminated, the for loop compares both vectors and returns true or false depending on whether any of the vector elements matches.

So my question: is there a simpler or clearer way to write this? Because it seems pertinent to remember if a match is found, because, without it, the nested for loops just prints out the words twice.

Onome Sotu
  • 666
  • 9
  • 18
  • 2
    C++98 didn't have `initializer_list` or constructors taking those. Why not compile using the current standard? – Bo Persson Mar 03 '17 at 19:09
  • I have tried getting the current build system (c++14) for my text editor sublime text, but I couldn't find it. The ones I found won't make the build on the bash terminal, so I decided to go along with this for a while instead of procrastinating learning. – Onome Sotu Mar 03 '17 at 19:14
  • 1
    The difference between C++98 and C++11 is huge. I would strongly recommend getting at least C++11 working. Many best practices of C++98 have been made obsolete or even wrong since C++11. – François Andrieux Mar 03 '17 at 19:25
  • C++14 is not a build system, it is the 2014 version of the C++ Standard. To request your compiler to conform to it, pass it the option `std=c++14`. If your compiler is too old it will not recognize this option. In that case pass it `std=c++11`, to request conformity with the 2011 standard. That will be sufficient to compile your initializer list. If your compiler is too old for that you should really upgrade it. – Mike Kinghan Mar 03 '17 at 19:26
  • Thanks@MikeKinghan. Here is my compiler info: `gcc (Ubuntu 5.4.0-6ubuntu1~16.04.4) 5.4.0 20160609`. I think it is up to date. But I run it via sublime text to the terminal. When I try changing the standard to c++14 or 11, it says no input file found..it is just so confusing. This is why I am stuck to learning with c++98 standard – Onome Sotu Mar 03 '17 at 19:27
  • @OnomeSotu *is there a simpler or clearer way to write this?* -- Not only simplify the declaration, but the `for` loops more than likely can be totally eliminated by using `std::mismatch`, or some other algorithm function(s). – PaulMcKenzie Mar 03 '17 at 19:30
  • 2
    @OnomeSotu *This is why I am stuck to learning with c++98 standard* -- So you're letting a buggy IDE and/or text editor determine how you will learn C++? – PaulMcKenzie Mar 03 '17 at 19:33
  • I am not yet up to algorithm functions yet, but I will take note of it. I shouldn't determine how I learn C++, I'm pretty ashamed. :( I will look for a solution soonest. – Onome Sotu Mar 03 '17 at 19:36
  • YES!!!! @PaulMcKenzie I solved it!! I found a way to use the c++11 standard just like @MikeKinghan suggested. It worked, as it turns out I can declare vectors in this format: `vector v = {"plan", "man", "canal"};`. Thanks for the encouragement and insight. – Onome Sotu Mar 03 '17 at 20:19
  • http://stackoverflow.com/questions/37118573/compiling-program-as-c-14-in-sublime-text-3-as-default to use c++14 in ST3 – Klaus Mar 03 '17 at 20:19
  • 1
    Sniff sniff I smell a Stroustrup challenge :). I think the disliked words need to be individual strings. Assuming this is from p.125, if I recall correctly, he sort of hints at this in the instructions. At this early stage in the book he hasn’t introduced any complex ways of accessing vector elements. –  Feb 16 '18 at 00:16

0 Answers0