BinarySearch List

Hey,

I'm trying to search a List for a substring, but even if it does exist the result is coming out as negative (not present) using the BinarySearch.

The idea is that once the file has been loaded into the list I search to see if substrings exist (i.e. IDS_STRING1 ) <-- includes tab. The lines in the file will say "IDS_STRING# "Something else written after tab"", but as anything could be written after the tab, that's why I need to search for the first part of the string only.

The text file is in Unicode (UTF-16 LE).

Can anyone see what I'm doing wrong in the code below? Sorry if it's really obvious, but I just can't see it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
List<String^>^ load = gcnew List<String^>();
// load file
{
	System::IO::StreamReader^ sr = gcnew System::IO::StreamReader(L"Chinese.txt",System::Text::Encoding::Unicode,true);
	String^ line1;
	while((line1 = sr->ReadLine()) !=nullptr)
	{
		load->Add(line1);
	}
	sr->Close();
	load->Sort();
}

// output to file
for(int i = 1; i < 1018; ++i)
{
	std::wostringstream integer;
	integer << L"IDS_STRING" << i << L"\x09";
	wstring thisString = integer.str();
	String^ SpareString = gcnew String (thisString.c_str());
	int index = load->BinarySearch(SpareString);
	System::IO::StreamWriter^ WriteSpareStrings = gcnew System::IO::StreamWriter(L"Chinese.txt", true, System::Text::Encoding::Unicode);
	if(index < 0)
	{
		WriteSpareStrings->Write(SpareString + L"\"Spare String\"");
		WriteSpareStrings->Write(L"\r\n");
	}
	WriteSpareStrings->Flush();
	WriteSpareStrings->Close();
}


Thanks!
Last edited on
closed account (o1vk4iN6)
Ugh, C++ .net, makes me sad each time I see someone using it. You'd probably have better luck posting at a .net forum since .net doesn't use any standard C and C++ libraries. Why are you mixing .Net and C++ libraries, you might as well use either String or std::string (not .net).

std::wostringstream integer; // why name this integer, only makes this confusing

Why are you using BinarySearch() what does that do, why not just recurse through the list and see if the string exists.

.Net String is always using wide-character so this is redundant:
1
2
wstring thisString = integer.str();
String^ SpareString = gcnew String (thisString.c_str());

Last edited on
Hi,

Yeah, I realised BinarySearch wasn't what I was looking for a while after I posted :S

Unfortunately, after trying for the last couple of hours, I can't work out how to just go through the list checking to see if it contains the generated string. Any pointers?

Thanks for the reply.
closed account (o1vk4iN6)
Rather than storing a pointer (which I assume ^ is by the syntax you are using) store it as the list as the actual variable.

 
List<String> list;


I'm not sure how different it is from C++, or I should say how similar it is C++ as compared to something like C#. If you are storing a pointer in the list I would assume if you did any type of built-in search it would be comparing pointers so it will always return -1 since it'd never be the same pointer.

By the sounds of it you should be able to use BinarySearch() from what I read, perhaps you could try Contains() as well. Again you'd need to make sure that it's checking the actual value and not a pointer.
Last edited on
I managed to fix my problem in the end by using a Dictionary and splitting the strings from the file.

Thanks for your help.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
Dictionary<String^,String^>^ load = gcnew Dictionary<String^,String^>();
// load file
{
	System::IO::StreamReader^ sr = gcnew System::IO::StreamReader(L"Chinese.txt",System::Text::Encoding::Unicode,true);
	String^ line1;
	array<System::String^>^ split = gcnew array<System::String^>(2);
	while((line1 = sr->ReadLine()) !=nullptr)
	{
		split = line1->Split(L'\t');
		String^ key = split[0];
		if(key->StartsWith(L"IDS"))
		{
			String^ value = split[1];
			load->Add(key,value);
		}
	}
	sr->Close();
}

// output to file
for(int i = 1; i < 1018; ++i)
{
	std::wostringstream integer;
	integer << L"IDS_STRING" << i;
	wstring thisString = integer.str();
	String^ SpareString = gcnew String (thisString.c_str());
	System::IO::StreamWriter^ WriteSpareStrings = gcnew System::IO::StreamWriter(L"Chinese.txt", true, System::Text::Encoding::Unicode);
	if(load->ContainsKey(SpareString)==false)
	{
		WriteSpareStrings->Write(SpareString + L"\t\"Spare String\"");
		WriteSpareStrings->Write(L"\r\n");
	}
	WriteSpareStrings->Flush();
	WriteSpareStrings->Close();
}
Topic archived. No new replies allowed.