64,648
社区成员
发帖
与我相关
我的任务
分享
struct studentData
{
studentData() {}
studentData(studentData &&data)
{
*this = std::move(data);
}
studentData& operator=(studentData &&data)
{
if(this != &data)
{
name_.swap(data.name_);
score_ = data.score_;
sex_.swap(data.sex_);
student_number_.swap(data.student_number_);
}
return *this;
}
studentData(std::string const &name, size_t score,
std::string const &sex, std::string const &student_number)
: name_(name), score_(score), sex_(sex), student_number_(student_number)
{}
std::string name_;
size_t score_;
std::string sex_;
std::string student_number_;
};
#ifndef INTERVIEW_00_HPP
#define INTERVIEW_00_HPP
#include <algorithm>
#include <iostream>
#include <iterator>
#include <string>
#include <vector>
struct studentData
{
studentData() {}
studentData(std::string const &name, size_t score,
std::string const &sex, std::string const &student_number)
: name_(name), score_(score), sex_(sex), student_number_(student_number)
{}
std::string name_;
size_t score_;
std::string sex_;
std::string student_number_;
};
std::ostream& operator<<(std::ostream &out, studentData const &data)
{
out << "score : " << data.score_ << std::endl;
out << "name : " << data.name_ << std::endl;
out << "student number : " << data.student_number_ << std::endl;
out << "sex : " << data.sex_ << std::endl;
return out;
}
bool operator==(studentData const &lhs, studentData const &rhs)
{
return lhs.score_ == rhs.score_;
}
bool operator<(studentData const &lhs, studentData const &rhs)
{
return lhs.score_ < rhs.score_;
}
class studentManager
{
public:
studentManager() {}
void add_student(studentData const &data)
{
students_.push_back(data);
}
void print_data()
{
std::for_each(std::begin(students_), std::end(students_),
[&](studentData const &fir)
{
std::cout<<fir<<std::endl;
} );
}
size_t ranking(size_t value)
{
auto const result = std::equal_range(std::begin(ranking_), std::end(ranking_), value);
if(result.first != result.second)
return ranking_.size() - (result.first - std::begin(ranking_) );
return 0;
}
void sort_by_score()
{
if(!students_.empty())
{
std::sort(std::begin(students_), std::end(students_) );
ranking_.clear();
std::for_each(std::begin(students_), std::end(students_),
[&](studentData const &fir)
{
ranking_.push_back(fir.score_);
} );
auto const it = std::unique(std::begin(ranking_), std::end(ranking_));
ranking_.erase(it, std::end(ranking_));
}
}
private:
studentManager(studentManager const&);
studentManager& operator=(studentManager const&);
private:
std::vector<size_t> ranking_;
std::vector<studentData> students_;
};
inline void test_student_manager()
{
studentManager manager;
manager.add_student(studentData("one", 99, "male", "1"));
manager.add_student(studentData("two", 99, "male", "2"));
manager.add_student(studentData("three", 100, "male", "3"));
manager.add_student(studentData("four", 77, "male", "4"));
manager.add_student(studentData("five", 88, "male", "5"));
manager.add_student(studentData("six", 88, "male", "6"));
manager.sort_by_score();
manager.print_data();
std::cout<< manager.ranking(100) << std::endl;
std::cout<< manager.ranking(99) << std::endl;
std::cout<< manager.ranking(88) << std::endl;
std::cout<< manager.ranking(77) << std::endl;
}
#endif // INTERVIEW_00_HPP
studentManager() {}
#ifndef INTERVIEW_01_HPP
#define INTERVIEW_01_HPP
#include <iostream>
inline void DrawImage(size_t image, size_t x, size_t y)
{
//std::cout << "(" << x << ", " << y << ") : " << image;
std::cout << image;
}
inline void test_draw_image()
{
size_t image[6] = {0, 1, 2, 3, 4, 5};
size_t map[10][10] =
{
{0, 1, 1, 2, 3, 1, 1, 2, 3, 4},
{5, 3, 1, 2, 4, 3, 0, 1, 1, 2},
{2, 2, 2, 2, 4, 1, 2, 1, 2, 1},
{1, 1, 1, 4, 2, 4, 2, 2, 0, 2},
{3, 1, 4, 1, 2, 2, 2, 4, 0, 0},
{4, 4, 1, 1, 2, 0, 1, 4, 0, 1},
{4, 1, 1, 0, 2, 4, 0, 0, 3, 0},
{2, 0, 0, 2, 2, 4, 2, 2, 4, 4},
{2, 0, 2, 2, 1, 0, 4, 3, 1, 0},
{2, 0, 0, 4, 1, 1, 2, 3, 3, 1}
};
for(int x = 0; x != 10; ++x)
{
for(int y = 0; y != 10; ++y)
{
DrawImage(image[map[x][y]], x, y);
}
std::cout << std::endl;
}
}
#endif // INTERVIEW_01_HPP
#ifndef INTERVIEW_00_HPP
#define INTERVIEW_00_HPP
#include <algorithm>
#include <iostream>
#include <iterator>
#include <string>
#include <vector>
struct studentData
{
studentData() = default;
studentData(std::string const &name, size_t score,
std::string const &sex, std::string const &student_number)
: name_(name), score_(score), sex_(sex), student_number_(student_number)
{}
std::string name_;
size_t score_;
std::string sex_;
std::string student_number_;
};
std::ostream& operator<<(std::ostream &out, studentData const &data)
{
out << "score : " << data.score_ << std::endl;
out << "name : " << data.name_ << std::endl;
out << "student number : " << data.student_number_ << std::endl;
out << "sex : " << data.sex_ << std::endl;
return out;
}
bool operator==(studentData const &lhs, studentData const &rhs)
{
return lhs.score_ == rhs.score_;
}
bool operator<(studentData const &lhs, studentData const &rhs)
{
return lhs.score_ < rhs.score_;
}
class studentManager
{
public:
studentManager() = default;
studentManager(studentManager const&) = delete;
studentManager& operator=(studentManager const&) = delete;
void add_student(studentData const &data)
{
students_.push_back(data);
}
void print_data()
{
for(studentData const &data : students_)
{
std::cout<< data << std::endl;
}
}
size_t ranking(size_t value)
{
auto const result = std::equal_range(std::begin(ranking_), std::end(ranking_), value);
if(result.first != result.second)
return ranking_.size() - (result.first - std::begin(ranking_) );
return 0;
}
void sort_by_score()
{
std::sort(std::begin(students_), std::end(students_) );
ranking_.clear();
for(studentData const &data : students_)
ranking_.push_back(data.score_);
auto it = std::unique(std::begin(ranking_), std::end(ranking_));
ranking_.erase(it, std::end(ranking_));
//for(size_t data : ranking_) std::cout << data << std::endl;
}
private:
std::vector<size_t> ranking_;
std::vector<studentData> students_;
};
inline void test_student_manager()
{
studentManager manager;
manager.add_student(studentData("one", 99, "male", "1"));
manager.add_student(studentData("two", 99, "male", "2"));
manager.add_student(studentData("three", 100, "male", "3"));
manager.add_student(studentData("four", 77, "male", "4"));
manager.add_student(studentData("five", 88, "male", "5"));
manager.add_student(studentData("six", 88, "male", "6"));
manager.sort_by_score();
manager.print_data();
std::cout<< manager.ranking(100) << std::endl;
std::cout<< manager.ranking(99) << std::endl;
std::cout<< manager.ranking(88) << std::endl;
std::cout<< manager.ranking(77) << std::endl;
}
#endif // INTERVIEW_00_HPP
#ifndef INTERVIEW_00_HPP
#define INTERVIEW_00_HPP
#include <algorithm>
#include <functional>
#include <iostream>
#include <iterator>
#include <string>
#include <vector>
struct studentData
{
studentData() = default;
studentData(std::string const &name, size_t score,
std::string const &sex, std::string const &student_number)
: name_(name), score_(score), sex_(sex), student_number_(student_number)
{}
std::string name_;
size_t score_;
std::string sex_;
std::string student_number_;
};
std::ostream& operator<<(std::ostream &out, studentData const &data)
{
out << "score : " << data.score_ << std::endl;
out << "name : " << data.name_ << std::endl;
out << "student number : " << data.student_number_ << std::endl;
out << "sex : " << data.sex_ << std::endl;
return out;
}
bool operator==(studentData const &lhs, studentData const &rhs)
{
return lhs.score_ == rhs.score_;
}
bool operator<(studentData const &lhs, studentData const &rhs)
{
return lhs.score_ > rhs.score_;
}
class studentManager
{
public:
studentManager() = default;
studentManager(studentManager const&) = delete;
studentManager& operator=(studentManager const&) = delete;
void add_student(studentData const &data)
{
students_.push_back(data);
}
void print_data()
{
for(studentData const &data : students_)
{
std::cout<< data << std::endl;
}
}
size_t ranking(size_t value)
{
auto const result = std::equal_range(std::begin(ranking_), std::end(ranking_),
value, std::greater<size_t>() );
if(result.first != result.second)
return result.first - std::begin(ranking_) + 1;
return 0;
}
void sort_by_score()
{
std::sort(std::begin(students_), std::end(students_) );
ranking_.clear();
for(studentData const &data : students_)
ranking_.push_back(data.score_);
auto it = std::unique(std::begin(ranking_), std::end(ranking_));
ranking_.erase(it, std::end(ranking_));
//for(size_t data : ranking_) std::cout << data << std::endl;
}
private:
std::vector<size_t> ranking_;
std::vector<studentData> students_;
};
inline void test_student_manager()
{
studentManager manager;
manager.add_student(studentData("one", 99, "male", "1"));
manager.add_student(studentData("two", 99, "male", "2"));
manager.add_student(studentData("three", 100, "male", "3"));
manager.add_student(studentData("four", 77, "male", "4"));
manager.add_student(studentData("five", 88, "male", "5"));
manager.add_student(studentData("six", 88, "male", "6"));
manager.sort_by_score();
manager.print_data();
std::cout<< manager.ranking(100) << std::endl;
std::cout<< manager.ranking(99) << std::endl;
std::cout<< manager.ranking(88) << std::endl;
std::cout<< manager.ranking(77) << std::endl;
}
#endif // INTERVIEW_00_HPP