스트링 리스트에서 스트링 찾기(find) for .NET with Visual C++/CLI
[파일명: TestStringFindInList.cpp]------------------------------------------------
// Filename: TestStringFindInList.cpp
//
// Compile: cl /clr TestStringFindInList.cpp
// Execute: TestStringFindInList
#using <mscorlib.dll>
#using <System.dll>
using namespace System;
using namespace System::Collections;
using namespace System::Collections::Generic;
void PrintArray(List<String^>^ arr);
bool Contains(String^ s)
{
return s->IndexOf("셋") >= 0;
}
int main(array<String^> ^args) {
List<String^>^ words = gcnew List<String^>( gcnew array<String^> { "하나", "둘", "셋", "넷", "다섯", "여섯" } );
int where;
Console::Write("list: ");
PrintArray(words);
where = words->FindIndex(gcnew Predicate<String^>(Contains));
if (where >= 0) {
Console::Write("발견! ");
Console::WriteLine("Next word of 셋 in list: {0}", words[where+1]);
}
Console::WriteLine("Sorting...");
words->Sort();
Console::Write("list: ");
PrintArray(words);
where = words->FindIndex(gcnew Predicate<String^>(Contains));
if (where >= 0) {
Console::Write("발견! ");
Console::WriteLine("Next word of 셋 in list: {0}", words[where+1]);
}
return 0;
}
void PrintArray(List<String^>^ arr) {
Console::Write("[");
for (int i = 0; i < arr->Count - 1; i++) {
Console::Write("{0}, ", arr[i]);
}
if (arr->Count > 0)
Console::Write("{0}", arr[arr->Count - 1]);
Console::WriteLine("]");
}
------------------------------------------------
컴파일> cl /clr testStringFindInList.cpp
실행> TestStringFindInList
list: [하나, 둘, 셋, 넷, 다섯, 여섯]
발견! Next word of 셋 in list: 넷
Sorting...
list: [넷, 다섯, 둘, 셋, 여섯, 하나]
발견! Next word of 셋 in list: 여섯