Версия для печати темы
Нажмите сюда для просмотра этой темы в оригинальном формате
Форум программистов > C/C++: Общие вопросы > Загрузка из файла и поиск


Автор: botasa 25.4.2012, 23:06
Есть код, мне надо загрузить слова в контейнер и сделать поиск. Пробую по всякому не хочет работать поиск слов вопше. В чем проблема может из за wstring ?? Как исправить пробему ... поджемает уже ппс курсак этот =(


Код

// test.cpp: определяет точку входа для консольного приложения.
//

#include "stdafx.h"
#include <set>
#include <vector>
#include <string>
#include <algorithm>
#include <iterator>
#include <iostream>
#include <fstream>

using namespace std;
 
typedef vector<wstring> Words;
 
struct WordsEntry
{
  unsigned id;
  Words words;
 
  explicit WordsEntry(unsigned theId)
    : id(theId)
  {
  }
 
  WordsEntry(unsigned theId, const Words& theWords)
    : id(theId)
    , words(theWords)
  {
  }
};
 
struct WordEntryLessById
{
  bool operator () (const WordsEntry& lhs, const WordsEntry& rhs) const
  {
    return lhs.id < rhs.id;
  }
};
 
typedef set<WordsEntry, WordEntryLessById> Set;
 
wostream& operator << (wostream& out, const Words& words)
{
  copy(words.begin(), words.end(),
  ostream_iterator<wstring, wstring::value_type>(out, L", "));
  return out;
}
 
void Process(const Set& keySet, const Set& assocSet, const wstring& word)
{
  wcout << L"-----------------------------------------------------" << endl;
  wcout << L"Processing of the '" << word << L"' word:" << endl;
  Set::const_iterator keyIter = keySet.end();
  for (Set::const_iterator it = keySet.begin(), end = keySet.end(); it != end; ++it)
  {
    if (it->words.end() != find(it->words.begin(), it->words.end(), word))
    {
      keyIter = it;
      break;
    }
  }
 
  if (keyIter == keySet.end())
  {
    wcout << L"The word is not found in key set." << endl;
    return;
  }
 
  Set::const_iterator assocIter = assocSet.find(WordsEntry(keyIter->id));
  if (assocIter == assocSet.end())
  {
    cout << L"The word is not found in associative set." << endl;
    return;
  }
  wcout << assocIter->words << endl;
}
 
int _tmain(int argc, _TCHAR* argv[])
{
  Set eng;
  Set rus;

  Words words;

  ifstream in1("eng.txt");
  while(!in1.eof())
  {
      string www;
      in1 >> www;
      wstring myword(www.begin(), www.end());
      words.push_back(myword);

  }
  eng.insert(WordsEntry(0, words));

  words.clear();

  ifstream in2("rus.txt");
  while(!in2.eof())
  {
      string www;
      in2 >> www;
      wstring myword(www.begin(), www.end());
      words.push_back(myword);

  }
  rus.insert(WordsEntry(0, words));


  /*
  Words words;
  words.push_back(L"11");
  words.push_back(L"22");
  words.push_back(L"33");
  eng.insert(WordsEntry(0, words));

  words.clear();
  words.push_back(L"xx");
  words.push_back(L"yy");
  words.push_back(L"zz");
  rus.insert(WordsEntry(0, words));
 
  words.clear();
  words.push_back(L"33");
  words.push_back(L"44");
  words.push_back(L"55");
  eng.insert(WordsEntry(1, words));
 
  words.clear();
  words.push_back(L"aa");
  words.push_back(L"bb");
  words.push_back(L"cc");
  rus.insert(WordsEntry(1, words));
 */
for(;;)
  {
      wstring word;
      wcin >> word;
      Process(eng, rus, word);
  }
  /*
  Process(eng, rus, L"11");
  Process(eng, rus, L"44");
  Process(rus, eng, L"aa");
  Process(rus, eng, L"cc");
  Process(eng, rus, L"xyz");
  Process(rus, eng, L"xyz");
 */

  system("pause");
  return 0;
}


Добавлено через 3 минуты и 38 секунд
этот код сам дописал,  мне кажется что проблема из за wstring но мне без него туго как-то. Мне кажеться что не работает сравнение, то есть если у меня в ней  "123" в самой программе присвоил и когда ввожу вручную и сравниваю оно не работает. 

ifstream in1("eng.txt");
  while(!in1.eof())
  {
      string www;
      in1 >> www;
      wstring myword(www.begin(), www.end());
      words.push_back(myword);
  }
  eng.insert(WordsEntry(0, words));
  words.clear();
  ifstream in2("rus.txt");
  while(!in2.eof())
  {
      string www;
      in2 >> www;
      wstring myword(www.begin(), www.end());
      words.push_back(myword);
  }
  rus.insert(WordsEntry(0, words));


for(;;)
  {
      wstring word;
      wcin >> word;
      Process(eng, rus, word);
  }

Powered by Invision Power Board (http://www.invisionboard.com)
© Invision Power Services (http://www.invisionpower.com)