is it possible to read dictionary files to vectorbools?

I think this much code will suffice:

// include necessary files
#include <algorithm> // for std::copy
#include <fstream>   // for std::ifstream
#include <iostream>  // for std::cout
#include <iterator>  // for std::ostream_iterator
#include <map>       // for std::map
#include <string>    // for std::string
#include <vector>    // for std::vector

// function to convert string containing 0s and 1s
// to std::vector<bool>
auto str_to_vec(std::string &&s) {

  // declare vector to return, and later insert into dictionary
  std::vector<bool> v;

  // extract characters from the string repeatedly
  for (auto ch : s)

    // push true in vector if character is '1' else false, I've
    // ignored checking if the string contains only 0s and 1s,
    // you may wish to add a validation check for that yourself
    v.push_back(ch == '1');

  // return the constructed vector
  return v;

// driver function
int main() {

  // open your file, replace filename with your dictionary file
  std::ifstream fin("my_dict.txt");

  // declare your dictionary
  std::map<char, std::vector<bool>> dict;

  // extract strings from the file repeatedly
  for (std::string str; fin >> str;)

    // check if length of string is atleast 2 you may skip this
    // if already guaranteed
    if (str.length() > 1)

      dict[str[0]] = str_to_vec(str.substr(1));

  // now do something with your dictionary here
  for (auto &&[ch, v] : dict) {
    std::cout << '\n' << ch << " : ";
    std::copy(v.begin(), v.end(),
              std::ostream_iterator<bool>(std::cout, ""));

Sample run :

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top