[파일명:  testStringFindInVector.cpp]------------------------------------------------
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>

using namespace std;

void printArray(vector<string>  arr);

int main() {
    int SIZE = 6;
    string data[] = { "하나", "둘", "셋", "넷", "다섯", "여섯" };
    vector<string> words(&data[0], &data[SIZE]);
    vector<string>::iterator it;

    /////////////////////////////////
    // words[0] = "하나";
    // words[1] = "둘";
    // words[2] = "셋";
    // words[3] = "넷";
    // words[4] = "다섯";
    // words[5] = "여섯";

    cout << "vector: ";
    printArray(words);

    it = find(words.begin(), words.end(), "셋");
    if (it != words.end()) {
        cout << "발견!  ";
        cout << "Next word of 셋 in vector: " << *(++it) << endl;
    }

    cout << "Sorting..." << endl;
    sort(words.begin(), words.end());

    cout << "vector: ";
    printArray(words);

    it = find(words.begin(), words.end(), "셋");
    if (it != words.end()) {
        cout << "발견!  ";
        cout << "Next word of 셋 in vector: " << *(++it) << endl;
    }
     return 0;
}

void printArray(vector<string> v) {
    cout << "[";
    for (int i = 0; i < v.size() - 1; i++) {
        cout << v[i] << ", ";
    }
    if (v.size() > 0)
        cout << v[v.size() - 1];
    cout << "]" << endl;
}
------------------------------------------------


Visual C++의 경우:
컴파일> cl /EHsc testStringFindInVector.cpp
또는
Borland C++의 경우:
컴파일> bcc32 testStringFindInVector.cpp
또는
Cywin이나 Msys의  g++의 경우:
컴파일> g++ -o testStringFindInVector testStringFindInVector.cpp

실행> testStringFindInVector
vector: [하나, 둘, 셋, 넷, 다섯, 여섯]
발견!  Next word of 셋 in vector: 넷
Sorting...
vector: [넷, 다섯, 둘, 셋, 여섯, 하나]
발견!  Next word of 셋 in vector: 여섯


Cywin이나 Msys의  g++의 경우:
실행> ./testStringFindInVector
vector: [하나, 둘, 셋, 넷, 다섯, 여섯]
발견!  Next word of 셋 in vector: 넷
Sorting...
vector: [넷, 다섯, 둘, 셋, 여섯, 하나]
발견!  Next word of 셋 in vector: 여섯



크리에이티브 커먼즈 라이선스
Creative Commons License

 

Posted by Scripter
,