我正在更新C ++编程技能,并且正在阅读Stroustrup的书,以学习使用STL和现代C ++习惯用法进行编码。我决定从编写实用程序类开始,该类Date应该支持基本的日期代数。我的Useful::Date类有一些静态的功能,我想作为来电Date.is_bus_day(),Date.plus_b_days(),Date.plus_b_months()等。
但是,由于某种原因,C ++似乎认为此函数是非静态函数而不是类函数。我不知道为什么会这样。我知道静态方法只能访问类的其他静态成员。
日期类
#include <iostream>
#include <string>
#include <vector>
template<typename T>
std::ostream& operator<<(std::ostream& os, const std::vector<T>& vec)
{
for (auto& el : vec)
{
os << el << ' ';
}
return os;
}
namespace Useful
{
typedef int Day;
typedef int Year;
enum Month{
January = 1,
February = 2,
March = 3,
April = 4,
May = 5,
June = 6,
July = 7,
August …Run Code Online (Sandbox Code Playgroud) 我是C ++的初学者。我想问一下:
是std::uniform_int_distribution<> dis(1, 6);等于std::uniform_int_distribution<int> dis(1,6);?
非常感谢。
我只是写这段代码,但我不知道它是如何工作的。
为什么价值k会是负数?怎么样?
#include<iostream>
using namespace std;
int main()
{
int k=1,j=0;
while(k>j)
{
j=k;
k++;
}
cout<<j<<"\n"<<k;
}
Run Code Online (Sandbox Code Playgroud)
它显示了整数的范围