Papers/programming

[C++] std::list find

tomato13 2007. 12. 6. 15:44

http://www.korone.net/bbs/board.php?bo_table=etc_misc&wr_id=82&page=3

 

#include <stdio.h>
#include <string>
#include <list>
#include <stdlib.h>
#include <algorithm>

class PERSON {
public:
    size_t m_no;
    std::string m_name;

public:
    PERSON(size_t no, const char* name)
    {
        m_no = no;
        m_name = name;
    }

    int operator==(const PERSON& p) const
    {
        return (p.m_no == m_no && p.m_name == m_name);
    }
};

typedef std::list<PERSON> PersonList;
typedef std::list<PERSON>::iterator PersonListIt;

int get_random(size_t lo, size_t hi)
{
    return (int)((double)rand() /
        ((double)RAND_MAX + 1) * (++hi - lo) + lo);
}

int main()
{
    PersonList list;

    srandom(getpid() * time(0));

    for (size_t i=0; i<10; i++)
    {
        list.push_back(PERSON(get_random(1, 100), "ddd"));
    }
    printf("orig...\n");
    PersonListIt it = list.begin();
    while (it != list.end())
    {
        PERSON& p = *it;
        printf("no:%d name:%s\n", p.m_no, p.m_name.c_str());
        it++;
    }

    PersonListIt findIt = find(list.begin(), list.end(), PERSON(4, "ddd"));
    if (findIt != list.end())
    {
        printf("found...\n");
    }

    return 0;
}

'Papers > programming' 카테고리의 다른 글

static class  (0) 2008.02.13
double pointer memory allocation  (0) 2008.01.21
time calculation  (0) 2007.11.25
simple makefile with macro definition  (0) 2007.11.03
type casting[읽는중]  (0) 2007.10.24