Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4015426
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T09:37:04+00:00 2026-05-20T09:37:04+00:00

I seem to be having issues with using find() with the STL map on

  • 0

I seem to be having issues with using find() with the STL map on different platforms. Here is my code to be complete:

#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <fstream>
#include <string>
#include <map>

using namespace std;

void constructDictionary(map<string,bool> &dict);
bool isInDictionary(string word, map<string,bool> &dict);

int main(void)
{

    map<string, bool> dictionary;
    constructDictionary(dictionary);
    map<string, bool>::iterator it = dictionary.begin();

    while(it != dictionary.end()){
        cout << it->first <<endl;
        it++;
    }

    string word;
    while(true){
        cout << "Enter a word to look up: " << endl;
        cin >> word;
        if(isInDictionary(word, dictionary))
            cout << word << " exists in the dictionary." << endl;
        else
            cout << word << " cannot be found in the dictionary." << endl;
    }

    return 0;
}

void constructDictionary(map<string,bool> &dict)
{
    ifstream wordListFile;
    wordListFile.open("dictionaryList.txt");
    string line;

    while(!wordListFile.eof()){
        getline(wordListFile, line);
        dict.insert(pair<string,bool>(line, true));
    }

    wordListFile.close();
}

bool isInDictionary(string word, map<string,bool> &dict)
{
    if(dict.find(word) != dict.end())
        return true;
    else
        return false;
}

isInDictionary() works fine if compiled using visual studio in windows, however, on ubuntu and g++, this only works for the last entry made into the map. Any other word that I query returns false. I don’t understand the discrepancy in this behavior. In both cases, the while statement at the beginning of main correctly prints out everything in the map to prove that everything’s there.

Any ideas? Thank you.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T09:37:04+00:00Added an answer on May 20, 2026 at 9:37 am

    Is the error in getline and line-endings in your input file? You might find on Linux it is adding an extra \r to each word.

    Assuming none of your words contain spaces, you can get around this by simply using:

    std::string word;
    while( wordListFile >> word )
    {
       if( !word.empty() )
       {
           // do the insert
       }
    }
    

    You can also use getline but “trim” the strings at either end. There is unfortunately no standard trim function. There are a few implementations around.

    You should probably use std::set as your collection type instead of this extra “bool” which is always true whenever there is an entry.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having a problem with my Seam code and I can't seem to figure
I seem to be having issues. I have a query string that has values
Seem to be having an issue with std::auto_ptr and assignment, such that the object
i'm having an issue with creating a query in oracle which doesnt seem to
Still having issues with this problem. Please help if you can. So I am
Having follow the tutorial at http://mithin.in/2009/06/22/using-iphone-sdk-mapkit-framework-a-tutorial/ to integrate the MapKit into my application. However,
I am having some issues with the TRichEdit. The first issue is if I
I have been having this issue for a while now and I cannot seem
I seem to be missing something about LINQ. To me, it looks like it's
There seem to be many ways to define singletons in Python. Is there a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.