C ++从两个迭代器创建一个列表对

时间:2017-10-16 10:43:44

标签: c++ c++11 listiterator

我有两个迭代器和一个带有元素字符串和size_t的列表 我想要做的是将我的student_id和成绩添加到结果列表中。

1 个答案:

答案 0 :(得分:1)

在您的解释和添加一些内容后,我认为您的错误在于使用std::string::compare。我测试了以下代码(避免使用PropertyInfo[] infos = yourObjectInstance.GetType().GetProperties(); int count = 0; for(int i = 0; i < infos.Length; i++) { if(infos[i].PropertyType == typeof(string)) { string stringValue = infos[i].GetValue(yourObjectInstance).ToString().Trim(); if(!string.IsNullOrEmpty(body)) { count++; } continue; } if(infos[i].GetValue(yourObjectInstance) != null) { count++; } } if(count == 0) { // Handle error } // Create record ,因为未定义addRegister):

Transcript

请注意我如何使用#include <iostream> #include <list> #include <algorithm> typedef struct Records_r { std::string name; // Name of the records std::string student_id; std::list<std::pair<std::string, size_t>> grades; // List of (course, grade) pairs } Records; typedef std::list<std::pair<std::string, size_t>>::const_iterator Personaliteraattori; std::list<std::pair<std::string, size_t>> findCourseResults(const std::list<Records>& registry, const std::string& course) { std::list<std::pair<std::string, size_t>> results; for (std::list<Records>::const_iterator it = registry.begin(); it != registry.end(); ++it) { for (Personaliteraattori itt = it->grades.begin(); itt != it->grades.end(); ++itt) { if (itt->first.compare(course) == 0) { results.push_back(std::make_pair(it->student_id, itt->second)); } } } return results; } int main() { std::string s = "Record of student_123456"; std::string id = "123456"; std::list<std::pair<std::string, size_t>> grades; grades.emplace_back("Math", 2); grades.emplace_back("Basic Math", 4); grades.emplace_back("Advanced Math", 5); grades.emplace_back("Math 1", 3); Records t; t.name = s; t.student_id = id; t.grades = grades; std::list<Records> reg; reg.push_back(t); t.name = "Record of student_34567"; t.student_id = "34567"; t.grades.clear(); t.grades.push_back(std::pair<std::string,size_t>("Math",1)); t.grades.push_back(std::pair<std::string,size_t>("Catalan",5)); t.grades.push_back(std::pair<std::string,size_t>("Basic Math",9)); reg.push_back(t); for (auto& tr : reg ) { std::cout << "Name: " << tr.name << std::endl; std::cout << "Student id: " << tr.student_id << std::endl; std::cout << "Grades:" << std::endl; std::for_each(tr.grades.begin(), tr.grades.end(), [](std::pair<std::string, size_t> e) { std::cout << e.first << " : " << e.second << std::endl; }); } while(true){ std::cout<<"Enter a lecture: "; std::string lecture; std::getline(std::cin, lecture); std::list<std::pair<std::string, size_t>> results = findCourseResults(reg,lecture); for (auto& tr: results) { std::cout<<"StudentId: "<<tr.first<<" grades: "<<tr.second<<std::endl; } } return 0; } 功能。此代码生成所需的输出:

std::string::compare