13 Replies - 1086 Views - Last Post: 28 January 2010 - 10:34 PM Rate Topic: -----

#1 brock129  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 12-January 10

Search through a list<string> for a string

Posted 27 January 2010 - 11:13 AM

Ok, here is the problem I am having. I am taking in a file (sample1) and storing each line in a list<string>. Then I am trying to iterate through it and search for a string, and if it finds it I would like the program to output to console and exit. However it is not working how i had hoped. Here is the code.

SAMPLE1

//This is one of the .txt files I am using for input.  It is a unicode dictionary I created with another program I wrote.

1 = : 1575 : 1587 : 1585 : 1575 : 1574 : 1740 : 1604 : 1740; 
2 = : 1705 : 1575 : 1576 : 1740 : 1606 : 1729; 
3 = : 1606 : 1746; 
4 = : 1594 : 1585 : 1576 : 1616; 
5 = : 1575 : 1585 : 1583 : 1606; 
6 = : 1705 : 1740; 
7 = : 1605 : 1578 : 1606 : 1575 : 1586 : 1593 : 1729; 
8 = : 1601 : 1589 : 1740 : 1604; 
9 = : 1705 : 1608; 
10 = : 1605 : 1586 : 1740 : 1583; 
11 = : 1576 : 1681 : 1726 : 1575 : 1606 : 1746; 
12 = : 1705 : 1746; 
13 = : 1605 : 1606 : 1589 : 1608 : 1576 : 1746; 
14 = : 1605 : 1606 : 1592 : 1608 : 1585 : 1740; 
15 = : 1583 : 1746; 
16 = : 1583 : 1740; 
17 = : 1729 : 1746; 
18 = : 1580 : 1587; 
19 = : 1587 : 1746; 
20 = : 1575 : 1587 : 1585 : 1575 : 1574 : 1740 : 1604; 
21 = : 1601 : 1604 : 1587 : 1591 : 1740 : 1606 : 1740; 
22 = : 1575 : 1608 : 1585; 
23 = : 1593 : 1604 : 1575 : 1602 : 1608 : 1722; 
24 = : 1575 : 1581 : 1575 : 1591 : 1729; 
25 = : 1576 : 1606 : 1583 : 1740; 
26 = : 1705 : 1585 : 1606 : 1575; 
27 = : 1670 : 1575 : 1729 : 1578 : 1575; 
28 = : 1729 : 1746 : 1748; 
29 = : 1575 : 1580 : 1604 : 1575 : 1587; 
30 = : 1605 : 1740 : 1722; 
31 = : 1575 : 1587; 
32 = : 1711 : 1574 : 1740; 
33 = : 1575 : 1587 : 1740; 
34 = : 1740 : 1729; 
35 = : 1601 : 1740 : 1589 : 1604 : 1729; 
36 = : 1576 : 1726 : 1740; 
37 = : 1705 : 1740 : 1575; 
38 = : 1711 : 1740 : 1575; 
39 = : 1705 : 1729; 
40 = : 1705 : 1729 : 1740 : 1722; 
41 = : 1662 : 1585; 
42 = : 1583 : 1585; 
43 = : 1576 : 1606 : 1575 : 1574 : 1746; 
44 = : 1580 : 1575 : 1574 : 1740 : 1722; 
45 = : 1711 : 1746; 
46 = : 1578 : 1575 : 1705 : 1729; 
47 = : 1575 : 1605 : 1585 : 1740 : 1705 : 1729; 
48 = : 1578 : 1588 : 1608 : 1740 : 1588; 
49 = : 1705 : 1575; 
50 = : 1575 : 1586 : 1575 : 1604 : 1729; 
51 = : 1605 : 1605 : 1705 : 1606; 
52 = : 1729 : 1608 : 1587 : 1705 : 1746; 
53 = : 1578 : 1581 : 1578; 
54 = : 1705 : 1729 : 1606 : 1575; 
55 = : 1608 : 1580 : 1729; 
56 = : 1601 : 1604 : 1587 : 1591 : 1740 : 1606 : 1740 : 1608 : 1722; 
57 = : 1576 : 1746 : 1670 : 1740 : 1606 : 1740; 
58 = : 1594 : 1605; 
59 = : 1608; 
60 = : 1594 : 1589 : 1729; 
61 = : 1662 : 1726 : 1740 : 1604; 
62 = : 1585 : 1729 : 1575; 
63 = : 1608 : 1729; 
64 = : 1587 : 1605 : 1580 : 1726; 
65 = : 1585 : 1729 : 1746; 
66 = : 1729 : 1740 : 1722; 
67 = : 1586 : 1605 : 1740 : 1606; 
68 = : 1602 : 1576 : 1590 : 1729; 
69 = : 1705 : 1585 : 1585 : 1729 : 1575; 
70 = : 1602 : 1740 : 1575 : 1605; 
71 = : 1575 : 1605 : 1606; 
72 = : 1593 : 1605 : 1604; 
73 = : 1605 : 1578 : 1575 : 1579 : 1585; 
74 = : 1729 : 1608 : 1585 : 1729 : 1575; 
75 = : 1729 : 1746 : 1748 : 1705 : 1575 : 1576 : 1740 : 1606 : 1729; 
76 = : 1575 : 1580 : 1604 : 1575 : 1587 : 1722; 
77 = : 1576 : 1575 : 1578; 
78 = : 1594 : 1608 : 1585; 
79 = : 1570 : 1740 : 1575; 
80 = : 1593 : 1604 : 1575 : 1602 : 1746; 
81 = : 1740 : 1729 : 1608 : 1583 : 1740; 
82 = : 1576 : 1587 : 1578 : 1740 : 1608 : 1722; 
83 = : 1580 : 1575; 
84 = : 1574 : 1746; 
85 = : 1740 : 1575; 
86 = : 1606 : 1729 : 1740 : 1722 : 1748; 
87 = : 1605 : 1593 : 1575 : 1605 : 1604 : 1746; 
88 = : 1587 : 1576; 
89 = : 1576 : 1681 : 1746; 
90 = : 1581 : 1575 : 1605 : 1740; 
91 = : 1583 : 1585 : 1605 : 1740 : 1575 : 1606; 
92 = : 1575 : 1582 : 1578 : 1604 : 1575 : 1601 : 1575 : 1578; 
93 = : 1662 : 1575 : 1574 : 1746; 
94 = : 1580 : 1575 : 1578 : 1746; 
95 = : 1729 : 1740 : 1722 : 1748 : 1575 : 1587; 
96 = : 1670 : 1606 : 1583; 
97 = : 1575 : 1740 : 1587 : 1746; 
98 = : 1605 : 1581 : 1585 : 1608 : 1605; 
99 = : 1729 : 1608; 
100 = : 1580 : 1608; 
101 = : 1601 : 1604 : 1587 : 1591 : 1740 : 1606; 
102 = : 1586 : 1740 : 1585 : 1616; 
103 = : 1575 : 1606 : 1578 : 1592 : 1575 : 1605; 
104 = : 1591 : 1585 : 1581; 
105 = : 1583 : 1587 : 1740 : 1608 : 1722; 
106 = : 1729 : 1586 : 1575 : 1585; 
107 = : 1588 : 1729 : 1585 : 1740; 
108 = : 1705 : 1657; 
109 = : 1705 : 1585; 
110 = : 1585 : 1729; 
111 = : 1711 : 1746 : 1748 : 1575 : 1587 : 1585 : 1575 : 1574 : 1740 : 1604 : 1740; 
112 = : 1575 : 1602 : 1608 : 1575 : 1605 : 1616; 
113 = : 1605 : 1578 : 1581 : 1583 : 1729; 
114 = : 1585 : 1662 : 1608 : 1585 : 1657; 
115 = : 1575 : 1740 : 1705; 
116 = : 1729 : 1740; 
117 = : 1585 : 1608 : 1586; 
118 = : 1576 : 1593 : 1583; 
119 = : 1604 : 1711 : 1575 : 1606 : 1746; 
120 = : 1601 : 1740 : 1589 : 1604 : 1746; 
121 = : 1594 : 1740 : 1585; 
122 = : 1602 : 1575 : 1606 : 1608 : 1606 : 1740; 
123 = : 1602 : 1585 : 1575 : 1585; 
124 = : 1583 : 1740 : 1578 : 1746; 
125 = : 1729 : 1608 : 1574 : 1746; 
126 = : 1605 : 1584 : 1605 : 1578; 
127 = : 1729 : 1746 : 1748 : 1583 : 1585 : 1740 : 1722; 
128 = : 1575 : 1579 : 1606 : 1575 : 1569; 
129 = : 1601 : 1608 : 1580; 
130 = : 1576 : 1583 : 1726; 
131 = : 1589 : 1576 : 1581; 
132 = : 1588 : 1583 : 1578; 
133 = : 1662 : 1587 : 1606 : 1583; 
134 = : 1578 : 1606 : 1592 : 1740 : 1605; 
135 = : 1575 : 1587 : 1604 : 1575 : 1605 : 1705; 
136 = : 1580 : 1729 : 1575 : 1583; 
137 = : 1575 : 1593 : 1604 : 1740 : 1648; 
138 = : 1585 : 1729 : 1606 : 1605 : 1575; 
139 = : 1576 : 1587 : 1575 : 1605; 
140 = : 1587 : 1593 : 1583 : 1740; 
141 = : 1711 : 1585 : 1601 : 1578 : 1575 : 1585; 
142 = : 1705 : 1585 : 1604 : 1740 : 1575; 
143 = : 1729 : 1746 : 1748 : 1575 : 1587 : 1585 : 1575 : 1574 : 1740 : 1604 : 1740; 
144 = : 1601 : 1608 : 1580 : 1740 : 1608 : 1722; 
145 = : 1580 : 1606 : 1740 : 1606; 
146 = : 1662 : 1606 : 1575 : 1729; 
147 = : 1711 : 1586 : 1740 : 1606; 
148 = : 1705 : 1740 : 1605 : 1662; 
149 = : 1581 : 1605 : 1604 : 1746; 
150 = : 1602 : 1740 : 1583; 
151 = : 1585 : 1705 : 1726 : 1575; 
152 = : 1578 : 1726 : 1575 : 1748; 
153 = : 1575 : 1601 : 1608 : 1575 : 1580; 
154 = : 1729 : 1740 : 1604 : 1740; 
155 = : 1705 : 1575 : 1662 : 1657 : 1585; 
156 = : 1657 : 1740 : 1606 : 1705; 
157 = : 1575 : 1587 : 1578 : 1593 : 1605 : 1575 : 1604; 
158 = : 1705 : 1574 : 1746 : 1748; 
159 = : 1578 : 1575 : 1729 : 1605; 
160 = : 1575 : 1606 : 1729 : 1740 : 1722; 
161 = : 1580 : 1575 : 1606 : 1576; 
162 = : 1705 : 1587 : 1740; 
163 = : 1602 : 1587 : 1605; 
164 = : 1605 : 1586 : 1575 : 1581 : 1605 : 1578; 
165 = : 1587 : 1575 : 1605 : 1606 : 1575; 
166 = : 1606 : 1729 : 1740 : 1722; 
167 = : 1662 : 1681 : 1575 : 1748; 



I then input that into this program...

SAMPLE 2
#include <iostream> 
#include <fstream> 
#include <string> 
#include <list>
#include <algorithm>
#include <functional>

using namespace std;

int main(int argc, char *argv[])
{	

	string dictFileName, dictSearchWord1, dictSearchWord2, dictSearchWord3, dictSearchWord4;
	
	if (argc == 2 && strncmp(argv[1],"french",6) == 0) 
	{

		dictFileName = "frenchUnicodeDictionary.txt";
		
	}
	
	if (argc == 2 && strncmp(argv[1],"german",6) == 0) 
	{

		dictFileName = "germanUnicodeDictionary.txt";
	
	}
	
	if (argc == 2 && strncmp(argv[1],"spanish",7) == 0) 
	{

		dictFileName = "spanishUnicodeDictionary.txt";
		
	
	}
	
	if (argc == 2 && strncmp(argv[1],"urdu",4) == 0) 
	{
	
		dictFileName = "urduUnicodeDictionary.txt";
	
	}
	
	string s;
	list<string> dict;
	ifstream dictFile(&dictFileName[0]);
	
	while ( !dictFile.eof()) 
	{ 
		getline(dictFile,s);
		dict.push_back(s);
	} 

	dictFile.close();
	
	s.clear();

	list<string>::iterator dictIter1, dictIter2, dictIter3, dictIter4;
	
	dictSearchWord1 = "= : 101 : 116;"; //and in french: et
	dictSearchWord2 = "= : 117 : 110 : 100;"; //and in german: und
	dictSearchWord3 = "= : 99 : 111 : 110;"; //with in spanish: con
	dictSearchWord4 = "= : 1575 : 1608 : 1585;"; //and in urdu: as letters(alef,waw,reh)
	
	
	dictIter1 = find(dict.begin(), dict.end(), &dictSearchWord1[0]);
	if(dictIter1 == &dictSearchWord1[0])
	{
		cout << "This file is French text" << endl;
	}
	dictIter2 = find(dict.begin(), dict.end(), &dictSearchWord2[0]);
	if(dictIter2 == &dictSearchWord2[0])
	{
		cout << "This file is German text" << endl;
	}
	dictIter3 = find(dict.begin(), dict.end(), &dictSearchWord3[0]);
	if(dictIter3 == &dictSearchWord3[0])
	{
		cout << "This file is Spanish text" << endl;
	}
	dictIter4 = find(dict.begin(), dict.end(), &dictSearchWord4[0]);
	if(dictIter4 == &dictSearchWord4[0])
	{
		cout << "This file is Urdu(arabic) text" << endl;
	}
	
}



Please help me find what I am doing wrong. Thank you ahead of time!

This post has been edited by brock129: 27 January 2010 - 02:42 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Search through a list<string> for a string

#2 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: Search through a list<string> for a string

Posted 27 January 2010 - 11:30 AM

So what is it doing/no-doing? (sorry I can't really run it at the moment)
Was This Post Helpful? 0
  • +
  • -

#3 brock129  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 12-January 10

Re: Search through a list<string> for a string

Posted 27 January 2010 - 11:45 AM

View PostNickDMax, on 27 Jan, 2010 - 10:30 AM, said:

So what is it doing/no-doing? (sorry I can't really run it at the moment)



Oh, sorry about that, i forgot to put in the error codes... Well in a nutshell it wont compile as it is right now after i changed a couple things and says
languageIdentity.cpp:75: error: no match for ‚operator==‚ in ‚dictIter1 == (char*)dictSearchWord1.std::basic_string<_CharT, _Traits, _Alloc>::operator[] [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>](0ul)‚
/usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/stl_list.h:169: note: candidates are: bool std::_List_iterator<_Tp>::operator==(const std::_List_iterator<_Tp>&) const [with _Tp = std::basic_string<char, std::char_traits<char>, std::allocator<char> >]



And it has that error for each similar line

This post has been edited by brock129: 27 January 2010 - 11:46 AM

Was This Post Helpful? 0
  • +
  • -

#4 Bench  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 855
  • View blog
  • Posts: 2,338
  • Joined: 20-August 07

Re: Search through a list<string> for a string

Posted 27 January 2010 - 12:04 PM

I think your code suffers from far too much "C", you've ended up with a really uneasy mix between two fundamentally different languages, but your code could be much, much simpler.

Whats with all the &varname[0] stuff? it seems to me that this is causing a compiler error (and later on it'll cause a logic error since a pointer is different to an iterator), I can't really understand why you think you need to do it - you can use == to compare strings
if( *dictIter1 == dictSearchWord1)



You've also got two problems here:
    ifstream dictFile(&dictFileName[0]);
   
    while ( !dictFile.eof())
    {
        getline(dictFile,s);
        dict.push_back(s);
    }  
this should be
    ifstream dictFile(dictFileName.c_str());
   
    while ( getline(dictFile,s) )
        dict.push_back(s);  
you can't use & [0] on a string and expect to yield a null-terminated string. it might appear to work, but its undefined behaviour.

As for reading a file, you can't use "eof" in a while loop, because it is likely to overflow beyond the end of the file (eof is not set when there is no more data - its set after a failed read attempt) - I wrote this summary of the problem a while ago - http://www.dreaminco...topic145699.htm

This post has been edited by Bench: 27 January 2010 - 12:05 PM

Was This Post Helpful? 1
  • +
  • -

#5 Anarion  Icon User is offline

  • The Persian Coder
  • member icon

Reputation: 282
  • View blog
  • Posts: 1,456
  • Joined: 16-May 09

Re: Search through a list<string> for a string

Posted 27 January 2010 - 12:07 PM

if(dictIter1 == &dictSearchWord1[0])

It should be:
if(*dictIter1 == dictSearchWord1[0])

:)

*Edit* Bench got me :D

This post has been edited by Anarion: 27 January 2010 - 12:11 PM

Was This Post Helpful? 0
  • +
  • -

#6 Bench  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 855
  • View blog
  • Posts: 2,338
  • Joined: 20-August 07

Re: Search through a list<string> for a string

Posted 27 January 2010 - 12:10 PM

Also, it would appear that you could simplify this long block down to 3 lines
    if (argc == 2 && strncmp(argv[1],"french",6) == 0)
    {

        dictFileName = "frenchUnicodeDictionary.txt";
       
    }
   
    if (argc == 2 && strncmp(argv[1],"german",6) == 0)
    {

        dictFileName = "germanUnicodeDictionary.txt";
   
    }
   
    if (argc == 2 && strncmp(argv[1],"spanish",7) == 0)
    {

        dictFileName = "spanishUnicodeDictionary.txt";
       
   
    }
   
    if (argc == 2 && strncmp(argv[1],"urdu",4) == 0)
    {
   
        dictFileName = "urduUnicodeDictionary.txt";
   
    } 
to:
    if( argc == 2 )
    {
        dictFileName = argv[1];
        dictFileName += "UnicodeDictionary.txt";
    }

    ifstream dictFile(dictFileName.c_str()); 



One more thing - you should get rid of the & and [0] from here too - again, its undefined behaviour (and you don't need it either).
dictIter1 = find(dict.begin(), dict.end(), dictSearchWord1); 

This post has been edited by Bench: 27 January 2010 - 12:16 PM

Was This Post Helpful? 0
  • +
  • -

#7 brock129  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 12-January 10

Re: Search through a list<string> for a string

Posted 27 January 2010 - 12:49 PM

Ok, great suggestions. I have changed it now as u can see below and it compiles just fine. however it does not output anything to console when it runs through it. I have checked to make sure those strings are in fact in the file in that format but I cannot get this program to do the same.

#include <iostream> 
#include <fstream> 
#include <string> 
#include <list>
#include <algorithm>
#include <functional>

using namespace std;

int main(int argc, char *argv[])
{	

	string dictFileName, dictSearchWord1, dictSearchWord2, dictSearchWord3, dictSearchWord4;
	
	if( argc == 2 )
	{
		dictFileName = argv[1];
		dictFileName += "UnicodeDictionary.txt";
	}
	
	string s;
	list<string> dict;
	ifstream dictFile(dictFileName.c_str());
	
	while (getline(dictFile,s)) 
	{ 
		dict.push_back(s);
	} 

	dictFile.close();
	
	s.clear();

	list<string>::iterator dictIter1, dictIter2, dictIter3, dictIter4;
	
	dictSearchWord1 = "= : 101 : 116;"; //and in french: et
	dictSearchWord2 = "= : 117 : 110 : 100;"; //and in german: und
	dictSearchWord3 = "= : 99 : 111 : 110;"; //with in spanish: con
	dictSearchWord4 = "= : 1575 : 1608 : 1585;"; //and in urdu: as letters(alef,waw,reh)
	
	
	dictIter1 = find(dict.begin(), dict.end(), dictSearchWord1);
	if(*dictIter1 == dictSearchWord1)
	{
		cout << "This file is French text" << endl;
	}
	dictIter2 = find(dict.begin(), dict.end(), dictSearchWord2);
	if(*dictIter2 == dictSearchWord2)
	{
		cout << "This file is German text" << endl;
	}
	dictIter3 = find(dict.begin(), dict.end(), dictSearchWord3);
	if(*dictIter3 == dictSearchWord3)
	{
		cout << "This file is Spanish text" << endl;
	}
	dictIter4 = find(dict.begin(), dict.end(), dictSearchWord4);
	if(*dictIter4 == dictSearchWord4)
	{
		cout << "This file is Urdu(arabic) text" << endl;
	}
	
}



Was This Post Helpful? 0
  • +
  • -

#8 brock129  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 12-January 10

Re: Search through a list<string> for a string

Posted 27 January 2010 - 02:44 PM

View Postbrock129, on 27 Jan, 2010 - 11:49 AM, said:

Ok, great suggestions. I have changed it now as u can see below and it compiles just fine. however it does not output anything to console when it runs through it. I have checked to make sure those strings are in fact in the file in that format but I cannot get this program to do the same.

#include <iostream> 
#include <fstream> 
#include <string> 
#include <list>
#include <algorithm>
#include <functional>

using namespace std;

int main(int argc, char *argv[])
{	

	string dictFileName, dictSearchWord1, dictSearchWord2, dictSearchWord3, dictSearchWord4;
	
	if( argc == 2 )
	{
		dictFileName = argv[1];
		dictFileName += "UnicodeDictionary.txt";
	}
	
	string s;
	list<string> dict;
	ifstream dictFile(dictFileName.c_str());
	
	while (getline(dictFile,s)) 
	{ 
		dict.push_back(s);
	} 

	dictFile.close();
	
	s.clear();

	list<string>::iterator dictIter1, dictIter2, dictIter3, dictIter4;
	
	dictSearchWord1 = "= : 101 : 116;"; //and in french: et
	dictSearchWord2 = "= : 117 : 110 : 100;"; //and in german: und
	dictSearchWord3 = "= : 99 : 111 : 110;"; //with in spanish: con
	dictSearchWord4 = "= : 1575 : 1608 : 1585;"; //and in urdu: as letters(alef,waw,reh)
	
	
	dictIter1 = find(dict.begin(), dict.end(), dictSearchWord1);
	if(*dictIter1 == dictSearchWord1)
	{
		cout << "This file is French text" << endl;
	}
	dictIter2 = find(dict.begin(), dict.end(), dictSearchWord2);
	if(*dictIter2 == dictSearchWord2)
	{
		cout << "This file is German text" << endl;
	}
	dictIter3 = find(dict.begin(), dict.end(), dictSearchWord3);
	if(*dictIter3 == dictSearchWord3)
	{
		cout << "This file is Spanish text" << endl;
	}
	dictIter4 = find(dict.begin(), dict.end(), dictSearchWord4);
	if(*dictIter4 == dictSearchWord4)
	{
		cout << "This file is Urdu(arabic) text" << endl;
	}
	
}




I am still having issues with the above code...I can't seem to get it to print anything out once it runs through the file. Is there something wrong with the find function or how I'm checking if it worked??? please help asap
Was This Post Helpful? 0
  • +
  • -

#9 David W  Icon User is offline

  • DIC supporter
  • member icon

Reputation: 281
  • View blog
  • Posts: 1,788
  • Joined: 20-September 08

Re: Search through a list<string> for a string

Posted 27 January 2010 - 07:31 PM

Take a look at this to see some working code:

#include <iostream>
#include <fstream>
#include <string>
#include <list>

using namespace std;

int main()
{    
    list<string> dict;
    
    string dictFileName = "UnicodeDictionary.txt"; // as per above ...
    ifstream dictFile( dictFileName.c_str() );
    
    string s;
    while( getline(dictFile, s) )
    {
        dict.push_back(s); // file is loaded into list line by line ...
    }
    
    dictFile.close();
    
    string dictSearchWord1, dictSearchWord2, dictSearchWord3, dictSearchWord4;
    dictSearchWord1 = "= : 101 : 116;"; //and in french: et
    dictSearchWord2 = "= : 117 : 110 : 100;"; //and in german: und
    dictSearchWord3 = "= : 99 : 111 : 110;"; //with in spanish: con
    dictSearchWord4 = "= : 1575 : 1608 : 1585;"; //and in urdu: as letters(alef,waw,reh)
   
    list <string> :: iterator dictIter;
    
    for( dictIter = dict.begin(); dictIter != dict.end(); ++dictIter )
        cout << *dictIter << endl;
        
    string::size_type t;
    
    for( dictIter = dict.begin(); dictIter != dict.end(); ++dictIter )
    {
        t = dictIter->find( dictSearchWord1, 0);
        if( t != string::npos )
        {
            cout << "This file is French text ..."<< endl;
            cout << "see line: " << *dictIter << endl;
            break;
        }
    }

    for( dictIter = dict.begin(); dictIter != dict.end(); ++dictIter )
    {
        t = dictIter->find( dictSearchWord2, 0);
        if( t != string::npos )
        {
            cout << "This file is German text ..."<< endl;
            cout << "see line: " << *dictIter << endl;
            break;
        }
    }

    for( dictIter = dict.begin(); dictIter != dict.end(); ++dictIter )
    {
        t = dictIter->find( dictSearchWord3, 0);
        if( t != string::npos )
        {
            cout << "This file is Spanish text ..."<< endl;
            cout << "see line: " << *dictIter << endl;
            break;
        }
    }

    for( dictIter = dict.begin(); dictIter != dict.end(); ++dictIter )
    {
        t = dictIter->find( dictSearchWord4, 0);
        if( t != string::npos )
        {
            cout << "This file is Urdu text ..."<< endl;
            cout << "see line: " << *dictIter << endl;
            break;
        }
    }

    cin.get();
}



Shalom,

David
~
http://developers-he...index.php/topic,46.0.html
Was This Post Helpful? 0
  • +
  • -

#10 brock129  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 12-January 10

Re: Search through a list<string> for a string

Posted 27 January 2010 - 08:19 PM

THANK YOU SO MUCH DAVID! It works perfectly!

I would love it if you could quickly explain to me what is going on in each of those loops at the bottom. I am unfamiliar with using "->" etc. thanks again!
Was This Post Helpful? 0
  • +
  • -

#11 David W  Icon User is offline

  • DIC supporter
  • member icon

Reputation: 281
  • View blog
  • Posts: 1,788
  • Joined: 20-September 08

Re: Search through a list<string> for a string

Posted 27 January 2010 - 08:28 PM

View Postbrock129, on 27 Jan, 2010 - 07:19 PM, said:

THANK YOU SO MUCH DAVID! It works perfectly!

I would love it if you could quickly explain to me what is going on in each of those loops at the bottom. I am unfamiliar with using "->" etc. thanks again!



an iterator is similar to a pointer ...

pointers hold addresses of variables / objects

(*iter).method( ... ) can also be coded as iter->method( ... )

http://en.wikipedia.org/wiki/Iterator

All of the standard container template types provide a rich and consistent set of iterator types. The syntax of standard iterators is designed to resemble that of ordinary C pointer arithmetic, where the * and -> operators are used to reference the element to which the iterator points, and pointer arithmetic operators like ++ are used to advance the iterator to the next element.

Iterators are usually used in pairs, where one is used for the actual iteration and the second serves to mark the end of the collection. The iterators are created by the corresponding container class using standard methods such as begin() and end(). The iterator returned by begin() points to the first element, while the iterator returned by end() is a special value that does not reference any element.

When an iterator is advanced beyond the last element it is by definition equal to the special end iterator value. The following example shows a typical use of an iterator.

ContainerType C; // Any standard container type, like std::list<sometype>
 
for (ContainerType::iterator it = C.begin(); it != C.end(); ++it) 
{
    std::cout << *it << std::endl;
}



Try this ... and see:

    for( dictIter = dict.begin(); dictIter != dict.end(); ++dictIter )
    {
        t = (*dictIter).find( dictSearchWord4, 0);
        if( t != string::npos )
        {
            cout << "This file is Urdu text ..."<< endl;
            cout << "see line: " << *dictIter << endl;
            break;
        }


This post has been edited by David W: 27 January 2010 - 08:39 PM

Was This Post Helpful? 0
  • +
  • -

#12 David W  Icon User is offline

  • DIC supporter
  • member icon

Reputation: 281
  • View blog
  • Posts: 1,788
  • Joined: 20-September 08

Re: Search through a list<string> for a string

Posted 27 January 2010 - 08:50 PM

Quote

I would love it if you could quickly explain to me what is going on in each of those loops at the bottom.... thanks again!



You are looping through each line stored in the list (from the file) ...

For each (long) line (a string) ...
you then test and see if the string you are looking for is contained in that long line( string )
if so ... you break out of the loop ... but first report 'found' ... and show the line where you found it
Was This Post Helpful? 0
  • +
  • -

#13 David W  Icon User is offline

  • DIC supporter
  • member icon

Reputation: 281
  • View blog
  • Posts: 1,788
  • Joined: 20-September 08

Re: Search through a list<string> for a string

Posted 27 January 2010 - 10:05 PM

Another simpler example may help ...

#include <list>
#include <iostream>

using namespace std;

int a_isIn_b_atPosition( const string& a, const string& b )
{
    size_t found = b.find(a);
/*
    if( found != string::npos )
        cout << "The first \"" << a << "\" was found at position "
             << int(found)+1 << endl;
*/
    if( found == string::npos ) // i.e. if( NOT_found ) return 0 ...
        return 0; 
    // else ...
    return int(found)+1;    // i.e. return position = index+1 ...
                            // (or the above 0 ... if NOT found)
}

int main()
{
    // create  an empty STL list to hold STL string objects ...
    list <string> L;

    // get some strings into the list ...
    L.push_back( "The big cat" );
    L.push_back( "Tom the cat" );
    L.push_back( "Bob the dog" );
    L.push_back( "Babs the rat" );
    L.push_back( "Scat the mat" );
    L.push_back( "cats, a cat ... and more cats" );

    // show the list ...
    list <string> :: iterator Iter;
    cout << "list L = \n\n";
    for( Iter = L.begin(); Iter != L.end(); ++Iter )
        cout << *Iter << endl;
    cout << endl;

    // see if some whole line exits in the list ...( of lines )
    Iter = find( L.begin(), L.end(), "Babs the rat" );
    if( Iter == L.end() )
        cout << "There is no \"Babs the rat\" in list L." << endl;
    else
        cout << "There is a \"" << *Iter << "\" in list L." << endl;
        
    cout << endl << endl;

    // see if some part of a line exists in any line in the list ...
    cout << "Searching for \"cat\" in each line of the " << L.size()
         << " lines of list L  ... \n\n";
    int pos;
    for( Iter = L.begin(); Iter != L.end(); ++Iter )
        if( pos = a_isIn_b_atPosition("cat", *Iter) ) // i.e. if pos > 0 ...
            cout << "In the line \"" << *Iter
                 << "\", \"cat\" was found at position " << pos << endl;
                 
    cin.get();
}


This post has been edited by David W: 28 January 2010 - 06:06 PM

Was This Post Helpful? 0
  • +
  • -

#14 David W  Icon User is offline

  • DIC supporter
  • member icon

Reputation: 281
  • View blog
  • Posts: 1,788
  • Joined: 20-September 08

Re: Search through a list<string> for a string

Posted 28 January 2010 - 10:34 PM

You might like to also see this shorter version (and the comments) ...

// findIfWordsInLinesFromFile.cpp

#include <iostream>
#include <fstream>
#include <string>
#include <list>

using namespace std;

const string dictSearchWords[] =
{
    "= : 101 : 116;",           //and in french: et
    "= : 117 : 110 : 100;",     //and in german: und
    "= : 99 : 111 : 110;",      //with in spanish: con
    "= : 1575 : 1608 : 1585;"   //and in urdu: as letters(alef,waw,reh)
};
const string language[] = {"French", "German", "Spanish", "Urdu"};
const string dictFileName = "UnicodeDictionary.txt";


int main()
{    
    // create an empty list of strings to hold all file lines
    list < string > dict;
    
    // open file to read ...
    ifstream dictFile( dictFileName.c_str() );
    
    // file is loaded into list line by line ...
    string nextLine;
    while( getline( dictFile, nextLine ) )
        dict.push_back( nextLine );
        
    dictFile.close();

    // show all lines in the file ... i.e. all strings in the list of strings
    list < string > :: const_iterator dictIter;
    for( dictIter = dict.begin(); dictIter != dict.end(); ++dictIter )
        cout << *dictIter << endl;

    cout << endl;

    // for each search word ... look in lines ... until you find that search word
    int numSearchWords = sizeof(dictSearchWords)/sizeof(dictSearchWords[0]);
    for( int i = 0; i < numSearchWords; ++i ) // for every search word ...
    {
        // for every line ...
        for( dictIter = dict.begin(); dictIter != dict.end(); ++dictIter )
        {
            // see if word is in this line ... 'pos' holds start index in line
            string::size_type pos = dictIter->find( dictSearchWords[i] );
            if( pos != string::npos ) // then the word is in this line ... so ...
            {
                cout << "The above file is " << language[i] << " text ... "
                     << "See line " << *dictIter <<endl;
                break; // since found HERE... break out of INNER for loop NOW
            }
        }
    }

    cout << "\nPress 'Enter' to continue ... " << flush;
    getline( cin, nextLine );
}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1