该程序具有用户输入name
/ age
对,然后使用类输出它们.这是代码.
#include "std_lib_facilities.h"
class Name_pairs
{
public:
bool test();
void read_names();
void read_ages();
void print();
private:
vector<string>names;
vector<double>ages;
string name;
double age;
};
void Name_pairs::read_names()
{
cout << "Enter name: ";
cin >> name;
names.push_back(name);
cout << endl;
}
void Name_pairs::read_ages()
{
cout << "Enter corresponding age: ";
cin >> age;
ages.push_back(age);
cout << endl;
}
void Name_pairs::print()
{
for(int i = 0; i < names.size() && i < ages.size(); ++i)
cout << names[i] << " …
Run Code Online (Sandbox Code Playgroud) 我正在尝试重载运算符<<作为模板类对的朋友,但我不断收到编译器警告说
friend declaration std::ostream& operator<<(ostream& out, Pair<T,U>& v) declares a non template function
Run Code Online (Sandbox Code Playgroud)
对于此代码:
friend ostream& operator<<(ostream&, Pair<T,U>&);
Run Code Online (Sandbox Code Playgroud)
作为推荐说,它给出了第二个警告
if this is not what you intended, make sure the function template has already been declared and add <> after the function name here
Run Code Online (Sandbox Code Playgroud)
这是函数定义
template <class T, class U>
ostream& operator<<(ostream& out, Pair<T,U>& v)
{
out << v.val1 << " " << v.val2;
}
Run Code Online (Sandbox Code Playgroud)
这是整个班级.
template <class T, class U>
class Pair{
public:
Pair(T v1, U v2) : val1(v1), val2(v2){} …
Run Code Online (Sandbox Code Playgroud) 我只是想重载一个+运算符,我得到这个编译器警告
reference to local variable 'tmp' returned
Run Code Online (Sandbox Code Playgroud)
这是重载的代码
const Int& Int::operator+(const Int& p) const
{
Int tmp = value + p.value;
return tmp;
}
Run Code Online (Sandbox Code Playgroud)
这是班级
class Int{
int value;
public:
Int() {} // default constructor
Int(int v) : value(v) {}
Int& operator=(const Int&);
const Int& operator+(const Int&) const;
};
Run Code Online (Sandbox Code Playgroud) 我在实现一个嵌套类时遇到了麻烦,该类的构造函数是用一些封闭类的私有数据成员初始化的.
例:
Header File:
class Enclosing {
//...Public members
//...Private members
int x, int y
class Inner; // Declaration for nested class
};
Impl. File:
// Stuff...
class Enclosing::Inner {
explicit Inner() : foo(x), bar(y) // foo and bar are data members of Inner
//...
};
Run Code Online (Sandbox Code Playgroud)
我收到一个invalid use of non-static data member
错误.当涉及到其封闭类的成员的嵌套类访问时,是否有一些我缺少的东西?
大多数人使用像这样的指针......
if ( p != NULL ) {
DoWhateverWithP();
}
Run Code Online (Sandbox Code Playgroud)
但是,如果指针因任何原因为空,则不会调用该函数.
我的问题是,如果不检查NULL可能更有益吗?显然,在安全关键系统上,这不是一个选项,但是如果程序在没有它的情况下仍可以运行,那么你的程序崩溃的荣耀比没有被调用的函数更明显.
关于第一个问题,在使用指针之前是否总是检查NULL?
其次,考虑你有一个将指针作为参数的函数,并且在整个程序的多个指针上多次使用此函数.你是否发现在函数中测试NULL更有利(好处是你不必在整个地方测试NULL),或者在调用函数之前在指针上测试(这样做的好处就是调用函数没有任何开销) )?
我正在尝试确定当一个对象有一些不会改变的特征时,最好的选择是什么,并且在整个函数中都需要它.
在我看来,静态成员的真正原因是拥有一个可以更改的变量,从而影响同一个类的所有其他对象.但是,我有人建议将类"不变量"作为静态const成员.我正在寻找有关建立类常量的推荐方法的一些见解,以及原因.
我知道时间(0)通常用于播种随机数生成器,并且当程序每秒运行一次以上时它只会成为一个问题.我想知道在生成随机数时要考虑哪些更好的种子.我在Windows上阅读了有关GetTickCount,timeGetTime和QueryPerformanceCounter的内容.几乎所有的操作都能满足这些要求,还是有更好的播种选择?
这是一个使用boost库的快速代码示例:
#include <iostream>
#include <boost/random.hpp>
using namespace std;
using namespace boost;
int main()
{
mt19937 randGen(42);
uniform_int<> range(0,100);
variate_generator<mt19937&, uniform_int<> > GetRand(randGen, range);
for (int i = 0; i < 30; ++i)
cout << GetRand() << endl;
}
Run Code Online (Sandbox Code Playgroud) 在包含标题时,我有一个关于"最佳实践"的问题.
显然包含防护保护我们不要在头文件或源文件中包含多个包含,所以我的问题是你是否觉得#include包含头文件或源文件中所有必需的头文件是有益的,即使其中一个头部已包含一个其他包括.这样做的原因是读者可以看到文件所需的一切,而不是寻找其他标题.
例如:假设使用了防护装置:
// Header titled foo.h
#include "blah.h"
//....
Run Code Online (Sandbox Code Playgroud)
.
// Header titled bar.h that needs blah.h and foo.h
#include "foo.h"
#include "blah.h" // Unnecessary, but tells reader that bar needs blah
Run Code Online (Sandbox Code Playgroud)
此外,如果头文件中不需要标题,但是在相关的源文件中需要标题,是否将它放在标题或源文件中?
我正在做一本书练习,写一个生成伪随机数的程序.我开始简单了.
#include "std_lib_facilities.h"
int randint()
{
int random = 0;
random = rand();
return random;
}
int main()
{
char input = 0;
cout << "Press any character and enter to generate a random number." << endl;
while (cin >> input)
cout << randint() << endl;
keep_window_open();
}
Run Code Online (Sandbox Code Playgroud)
我注意到每次运行程序时都会有相同的"随机"输出.所以我调查了随机数生成器并决定尝试播种,首先在randint()中包含它.
srand(5355);
Run Code Online (Sandbox Code Playgroud)
这只是反复生成相同的数字(我现在感觉很愚蠢.)
所以我认为我会聪明并实现这样的种子.
srand(rand());
Run Code Online (Sandbox Code Playgroud)
这基本上只是与程序在第一时间做的相同,但输出了一组不同的数字(这是有道理的,因为rand()生成的第一个数字总是41.)
我能想到的唯一让它更随机的是:
我是否在我脑海中,我现在应该停下来吗?选项2难以实施吗?还有其他想法吗?
提前致谢.
该程序从txt文件中读取数字字符串,将它们转换为整数,将它们存储在向量中,然后尝试以有组织的方式输出它们,如此....
如果txt文件说:
7 5 5 7 3 117 5
Run Code Online (Sandbox Code Playgroud)
该方案产出:
3
5 3
7 2
117
Run Code Online (Sandbox Code Playgroud)
因此,如果数字不止一次出现,它会输出发生的次数.这是迄今为止的代码.
#include "std_lib_facilities.h"
int str_to_int(string& s)
{
stringstream ss(s);
int num;
ss >> num;
return num;
}
int main()
{
cout << "Enter file name.\n";
string file;
cin >> file;
ifstream f(file.c_str(), ios::in);
string num;
vector<int> numbers;
while(f>>num)
{
int number = str_to_int(num);
numbers.push_back(number);
}
sort(numbers.begin(), numbers.end());
for(int i = 0; i < numbers.size(); ++i)
{
if(i = 0 && numbers[i]!= numbers[i+1]) cout << …
Run Code Online (Sandbox Code Playgroud)