- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我目前正在学习 Accelerated C++,我有点难以理解我必须在哪里放置 #include(无论是在头文件和/或源文件中),而且大多数情况下,我是否正在多余的。
对于我应该更改的内容和不必要的内容的所有评论,我将不胜感激。我觉得我多次包含库(例如,如果主程序有#include,我是否需要将它包含在任何其他头文件/源文件中?)。另外,如果使用
#include "Student_info.h"
并且 Student_info.cpp 文件包含 grade.h,我是否仍需要在 main.cpp 中包含 grade.h 或者这是否足够?我发布这段代码是因为我认为应用到一个特定的例子是在我的脑海中钻研组织这些文件的正确方法以及我应该注意的常见错误的最好方法
文件列表 - 为了完整起见,我包含了整个代码,但只有最重要的部分和原则才是真正重要的
main.cpp
#include<iostream>
#include<string>
#include<ios>
#include<stdexcept>
#include<vector>
#include<algorithm>
#include<iomanip>
#include "grade.h"
#include "Student_info.h"
#include "analysis.h"
using std::cin; using std::setprecision;
using std::cout; using std::sort;
using std::domain_error; using std::streamsize;
using std::endl; using std::string;
using std::max; using std::vector;
int main()
{
//students who did and didn't do their homework
vector<Student_info> did, didnt;
//read the student records and partition them
Student_info student;
while (read(cin,student))
{
if (did_all_hw(student))
did.push_back(student);
else
didnt.push_back(student);
}
//verify that the analyses will show us something
if (did.empty())
{
cout<<"No student did all the homework!"<<endl;
return 1;
}
if (didnt.empty())
{
cout<<"Every student did all the homework!"<<endl;
return 1;
}
//do the analyses
write_analysis(cout, "median",median_analysis, did, didnt);
write_analysis(cout, "average",average_analysis, did, didnt);
write_analysis(cout, "median of homework turned in", optimistic_median_analysis, did, didnt);
return 0;
}
等级.h
#ifndef GUARD_grade_h
#define GUARD_grade_h
//grade.h - Header file
#include<vector>
#include "Student_info.h"
double grade(double, double, double);
double grade(double, double, const std::vector<double>&);
double grade(const Student_info&);
bool fgrade(const Student_info& s);
bool pgrade(const Student_info& s);
double grade_aux(const Student_info& s);
double average(const std::vector<double>& v);
double average_grade(const Student_info& s);
double optimistic_median(const Student_info& s);
#endif
等级.cpp
#include<stdexcept>
#include<vector>
#include<numeric>
#include "grade.h"
#include "median.h"
#include "Student_info.h"
using std::domain_error;
using std::vector;
//reference to constant vector of type double
//vector<double> homework
//vector<double>& hw = homework; (this means that hw is a synonym for homework)
//This function is a ALSO called grade
//This is OVERLOADING and the argument list will be checked to decide which function to call
//The & asks the implementation NOT to copy its argument and const means don't change it
double grade(double midterm, double final, const vector<double>& hw)
{
if (hw.size()==0)
throw domain_error("student has done no homework");
return grade(midterm,final,median(hw));
}
double grade(double midterm, double final, double homework)
{
return 0.2*midterm+0.4*final+0.4*homework;
}
//because it's a reference variable, there is no overboard in copying the object
//and modifying it in the function modifies the original variable passed in
//returns double representing OVERALL GRADE
double grade(const Student_info& s)
{
return grade(s.midterm,s.final,s.homework);
}
bool fgrade(const Student_info& s)
{
return grade(s) < 60;
}
bool pgrade(const Student_info& s)
{
return !fgrade(s);
}
//need this function because the transform function can't use grade as there are too many overloaded versions
//and it doesn't know which one to call
double grade_aux(const Student_info& s)
{
try {
return grade(s);
}
catch (domain_error)
{
return grade(s.midterm, s.final, 0);
}
}
//Write the average function to use for average_analysis
double average(const vector<double>& v)
{
//accumulate is defined in the <numeric> library
//The accumulate function adds the values in range denoted by the first two arguments, starting with the
//the value given by the third argument (which also gives the resulting type, e.g. 0.0 will make accumulate return a double
return accumulate(v.begin(),v.end(),0.0)/v.size();
}
double average_grade(const Student_info& s)
{
return grade(s.midterm,s.final, average(s.homework));
}
double optimistic_median(const Student_info& s)
{
vector<double> nonzero;
//extracts nonzero vectors from the homework vector and appends them to (vector<double> nonzero)
remove_copy(s.homework.begin(), s.homework.end(),
back_inserter(nonzero),0);
if (nonzero.empty())
return grade(s.midterm,s.final,0);
else
return grade(s.midterm, s.final,median(nonzero));
}
分析.h
#ifndef GUARD_output_analysis
#define GUARD_output_analysis
//output_analysis.h - header file
#include<iostream>
#include<vector>
#include<iterator>
#include "Student_info.h"
#include "grade.h"
#include "median.h"
using std::ostream; using std::string;
void write_analysis(ostream& out,
const string& name,
double analysis(const vector<Student_info>&),
const vector<Student_info>& did,
const vector<Student_info>& didnt);
double median_analysis(const vector<Student_info>& students);
double average_analysis(const vector<Student_info>& students);
double optimistic_median_analysis(const vector<Student_info>& students);
#endif
分析.cpp
#include<iterator>
#include<algorithm>
#include "Student_info.h"
#include "grade.h"
#include "median.h"
using std::istream; using std::vector;
using std::ostream; using std::string;
using std::endl; using std::transform;
void write_analysis(ostream& out,
const string& name,
double analysis(const vector<Student_info>&),
const vector<Student_info>& did,
const vector<Student_info>& didnt)
{
out <<name <<" : median(did) = "<<analysis(did)
<<", median(didnt) = "<<analysis(didnt)<<endl;
}
double median_analysis(const vector<Student_info>& students)
{
vector<double> grades;
//The function grade is applied to every element in the range students.begin() to students.end()
//and this is appended to the end of the grades vector
transform(students.begin(), students.end(), back_inserter(grades), grade_aux);
return median(grades);
}
double average_analysis(const vector<Student_info>& students)
{
vector<double> grades;
//recall that tranform applies the average_grade function to every element between students.begin() and students.end()
//and appends this to the end of the grade vector (which is where the iterator returned by back_inserter(grades) points to
transform(students.begin(), students.end(),
back_inserter(grades), average_grade);
return median(grades);
}
double optimistic_median_analysis(const vector<Student_info>& students)
{
vector<double> grades;
transform(students.begin(),students.end(), back_inserter(grades),optimistic_median);
return median(grades);
}
中位数.h
#ifndef GUARD_median_h
#define GUARD_median_h
//median.h
#include<vector>
double median(std::vector<double>);
#endif
中位数.cpp
#include "median.h"
using std::vector;
using std::sort;
using std::domain_error;
double median(vector<double> vec)
{
typedef vector<double>::size_type vec_sz;
vec_sz size=vec.size();
//if the vector is empty, an exception is thrown
//execution stops and passes to another part of the program
//along with the exception object
if (size==0)
throw domain_error("median of an empty vector");
sort(vec.begin(),vec.end());
vec_sz mid = size/2;
return size%2==0 ? ((vec[mid]+vec[mid-1])/2) : (vec[mid]);
}
Student_info.h
#ifndef GUARD_Student_info
#define GUARD_Student_info
//Student_info.h header file
#include<iostream>
#include<string>
#include<vector>
using std::vector;
struct Student_info {
std::string name;
double midterm,final;
std::vector<double> homework;
};
bool compare(const Student_info&, const Student_info&);
bool did_all_hw(const Student_info& s);
bool pgrade(const Student_info& s);
std::istream& read(std::istream&, Student_info&);
std::istream& read_hw(std::istream&, std::vector<double>&);
vector<Student_info> extract_fails(vector<Student_info>& students);
#endif
Student_info.cpp
//Source file for Student_info related functions
#include "Student_info.h"
#include "grade.h"
using std::istream; using std::vector;
bool compare(const Student_info& x, const Student_info& y)
{
return x.name < y.name;
}
istream& read(istream& is, Student_info& s)
{
//read and store the student's name and midterm and final exam grades
is >> s.name >> s.midterm >> s.final;
read_hw(is, s.homework); //read AND store all the student's homework grades
return is;
}
istream& read_hw(istream& in, vector<double>& hw)
{
if (in) {
//remove previous contents and leaves us with an empty vector
hw.clear();
double x;
while (in>>x)
hw.push_back(x);
//clear the stream so that input will work for the next student
//reset any error indicatiosn so that input can continue
in.clear();
}
//this means we were given an object that we're NOT going to a copy and we will
//return the object without copying it
return in;
}
bool did_all_hw(const Student_info& s)
{
//recall that if 0 is not found in the sequence given by the first two iterators, then the 2nd argument is returned
return ((find(s.homework.begin(), s.homework.end(), 0)) == s.homework.end());
}
//ONE-PASS SOLUTION
vector<Student_info> extract_fails(vector<Student_info>& students)
{
vector<Student_info>::iterator iter=stable_partition(students.begin(),students.end(),pgrade);
vector<Student_info> fail(iter,students.end()); //vector consisting of all failing elements
students.erase(iter,students.end());
return fail;
}
最佳答案
如果编译器需要知道你正在使用的类型的大小,或者如果你试图访问它的任何成员或方法(包括你没有显式创建的方法,比如作为默认构造函数、析构函数或赋值运算符)。在 Grade.h
中,您不需要 #include "Student_info.h"
因为 Student_info
是一个引用。您可以改用前向声明:
类 Student_info;//不要#include 它,转发声明它
此外,您几乎不需要编写 header 保护,因为所有现代编译器都支持 #pragma once
,它在编译期间只包含该文件一次。
关于c++ - 在代码中放置#includes 和冗余的位置(头文件和实现文件),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21882551/
我正在尝试获取从过去的 startposition/location 到当前移动的 currentposition/location 的距离(以米为单位)。 我确实有工作正常的currentposit
所以我有一堆绝对覆盖的 div。用户通过在叠加层上拖动来创建方形 div。如果您要创建一个 div,然后放大和缩小,div 会保持在同一位置,因为它对叠加层是绝对的,如前所述。 然而问题就出在这里。您
我想找到 View 在显示屏幕上的位置。 为此,我使用了 view.getLeft() 、view.getBottom() 、view.getRight() 等方法> , view.getTop()。
我有一个看起来像这样的 View 层次结构(基于其他答案和 Apple 的使用 UIScrollView 的高级 AutoLayout 指南): ScrollView 所需的2 个步骤是: 为 Scr
所以我有一个名为 MARKS 的表,我有这些列 STUDENT_ID, CLASSFORM_NAME, ACADEMIC_YEAR, TERM, SUBJECT_NAME, TOTAL_MARKS
我有一个问题我无法理解,请帮助: 我开发了带有图像的 html 页面,并使用 jQuery UI 帮助使它们可拖动,我将这些图像位置设置为相对位置并给出了左侧和顶部像素,这是页面的链接 http://
我正在尝试创建一个 CSS 动画,它在 sprite 表中循环播放 16 个图像,给人一种幽灵“漂浮”的错觉。动画通过在 background-position 位置之间移动以显示不同状态的幽灵来实现
我正在创建这个网站的 WebView https://nearxt.com/打开时询问位置但是当我使用此链接在 flutter 中创建 webview 时那么它就无法定位我还在应用程序中定义了位置,但
我正在以编程方式创建一个需要跨越 2 个屏幕的窗口。正在创建的窗口的大小是正确的,但窗口大约从第一个屏幕的一半开始。我可以将它拖回第一个屏幕的开头,NSWindow 非常适合。 我只需要知道在窗口的起
位置“/”的匹配叶路由没有元素。这意味着默认情况下它将呈现一个空值,从而导致一个“空”页面 //App.js File import { BrowserRouter as Router, Routes
我有一个运行 Ubuntu 和 Apache 的 VPS 例如,假设地址是:5.5.5.5 在 VPS 上,我有一个名为 eggdrop 的用户(除了我的 root 用户)。 用户 eggdrop 有
我有一个 JLabel与 ImageIcon ,我使用 setIcon() JLabel中的函数. ImageIcon然后上来,坐在我的JLabel 的文字左侧.是否有可能拥有 ImageIcon在文
我的图中有节点,它们的 xlabels 位于它们的左上方。我怎样才能改变这个位置?我希望 xlabels 正好位于节点本身的旁边。 最佳答案 xlp是你想要的属性,但它没有做任何事情。 你不能改变位置
我对基本的 VIM 功能有疑问:(我尝试谷歌搜索但找不到答案) 如何列出所有自定义功能。(我做了 :function 并且不能找到我的自定义函数) 如何获得自定义函数列表中的函数(或它们的存储位置)。
我是 PHP 的新手,虽然我一直在搜索,但我不知道该怎么做。 我知道可以使用 Location("some page") 进行重定向。我还读到,只要没有向用户显示任何内容,它就可以工作。 我想做的是:
如果在 jgrowl.css 中位置更改为“center”,我如何将其覆盖为默认值,即“top-right” $.jGrowl(data, { header: 'data', an
我需要根据用户是否滑动屏幕顶部、屏幕中间或屏幕底部来触发不同的事件。我正在尝试找出最好/最简单的方法来做到这一点,因为我很确定没有办法从 UISwipeGestureRecognizer 获取位置。
我需要枚举用delphi编写的外部应用程序中使用的类 ,因此我需要访问VMT表以获取该信息,但是我找不到任何有关如何在exe(由delphi生成)文件中找到VMT(虚拟方法表)的位置(地址)的文档。
在 D2010 (unicode) 中是否有像 Pos 这样不区分大小写的类似函数? 我知道我可以使用 Pos(AnsiUpperCase(FindString), AnsiUpperCase(Sou
我正在尝试为我的reveal.js 演示文稿制作一个标题,该标题会粘贴在屏幕顶部。标题中的内容在每张幻灯片的基础上都是动态的,因此我必须将标记放在 section 标记中。 显然,如果标记在 sect
我是一名优秀的程序员,十分优秀!