|
发表于 2015-11-13 12:47:55
|
显示全部楼层
- void splitstr(const char* str, vector<string>* vec)
- {
- const char* iter = str;
- const char* pos = str;
- while (*iter != NULL)
- {
- pos = strchr(iter, ' ');
- if (pos == NULL) // last char
- {
- vec->push_back(iter);
- return;
- }
- char buf[0xF] = {0};
- memcpy(buf, iter, pos - iter);
- iter = pos+1;
- vec->push_back(buf);
- }
- }
- #include <iostream>
- #include <vector>
- #include <algorithm>
- using namespace std;
- class stu
- {
- public:
- stu(string a_name, int a_number, int a_soure) : name(a_name), number(a_number), soure(a_soure){}
- friend ostream& operator<<(ostream& os, const stu& student)
- {
- os << student.name << ' ' << student.number << ' ' << student.soure << endl;
- return os;
- }
- string name;
- int number;
- int soure;
- };
- bool IsFail(const stu& student)
- {
- return student.soure < 60;
- }
- #include <iostream>
- using namespace std;
- int main()
- {
- vector<stu> vec;
- vec.reserve(0xFF);
- char buff[0xFF] = {0};
- printf("enter the [name] [number] [soure]\n");
- while(gets(buff) != NULL)
- {
- vector<string> tmp;
- splitstr(buff, &tmp);
- vec.push_back(stu(*tmp.begin(), atoi((*(tmp.begin()+1)).c_str()), atoi((*tmp.rbegin()).c_str())));
- cout << "the list is" << endl;
- copy(vec.begin(), vec.end(), ostream_iterator<stu>(cout, ""));
- printf("\n");
- cout << "the Fail list is "<< endl;
- copy_if(vec.begin(), vec.end(), ostream_iterator<stu>(cout, ""), IsFail);
- printf("\n");
- printf("enter the [name] [number] [soure]\n");
- }
- return 0;
- }
复制代码 |
|