我一直习惯cout打印声明,但现在我想学习打印passing the stream,就像 void print(std::ostream&) const;我目前的打印功能一样
template <class T>
void Mystack<T>::print()
{
for (int i = 0; i <= top; i++)
{
std::cout << input[i] << " ";
}
}
Run Code Online (Sandbox Code Playgroud)
我有两个问题:
ostream.ostream在我的功能中实现.我试图ostream从互联网来源了解但无法理解.请帮忙.以下是完整的运行代码:
//*************STACK CODE***************//
//VERY GOOD EXAMPLE TO UNDERSTAND RULE OF THREE FOR BEGINEERS http://www.drdobbs.com/c-made-easier-the-rule-of-three/184401400
//RULE OF THREE : Video : https://www.youtube.com/watch?v=F-7Rpt2D-zo
//Thumb Rule : Whenever we have class which has members pointing to heap space we …Run Code Online (Sandbox Code Playgroud) 我正在学习C++.我的教授使用了一些类似的代码
using filePath = std::string;
using setOfPaths = std::set<filePath>;
using iterOfSet = setOfPaths::iterator;
using listOfIter = std::list<iterOfSet>;
using iterList = listOfIter::iterator;
using fileName = std::string;
using mapOfFileName = std::map<fileName, listOfIter>;
using iterOfMap = mapOfFileName::iterator;
setOfPaths _setOfPaths;
mapOfFileName _mapOfFileName;
iterOfSet setIter;
Run Code Online (Sandbox Code Playgroud)
我想知道为什么我们使用using关键字.为什么我们不能简单地写
std::string filepath;
std::set<filepath> setOfPaths;
...
...
Run Code Online (Sandbox Code Playgroud)
拥有using关键字有什么好处?
我编写了一个用于实现堆栈的程序.我有一个显示功能.
这是我最初编写显示功能的方式:
template <class t>
void Mystack<t>::display()
{
for (int i = 0; i <= top; i++)
{
std::cout << input[i] << " ";
}
}
Run Code Online (Sandbox Code Playgroud)
然后我被开发人员建议编写一个更通用的显示函数.所以我写了显示功能:
template <class T>
void Mystack<T>::display(std::ostream &os) const
{
for (int i = 0; i <= top; i++)
{
os << input[i] << " ";
}
}
Run Code Online (Sandbox Code Playgroud)
根据我的理解,编写上述功能的好处是,现在我有一个通用的显示功能,我可以使用它来向控制台或文件显示数据.
问题1:我的理解是否正确?
现在另一个建议是写函数类似于:
template <typename T>
friend std::ostream& operator<<(std::ostream& s, Mystack<T> const& d) {
d.display(s);
return s;
}
Run Code Online (Sandbox Code Playgroud)
问题2:具有上述显示功能有什么好处?通过具有上述显示功能,我能够实现什么?
PS:我是编程新手,所以请用简单的语言回答我的疑惑.我找到了几个答案,但无法理解它们.下面是复制构造函数和赋值运算符重载.
template <class T>
Mystack<T>::Mystack(const Mystack<T> &source) // copy constructor
{
input = new T[source.capacity];
top = source.top;
capacity = source.capacity;
for (int i = 0; i <= source.top; i++)
{
input[i] = source.input[i];
}
}
template <class T>
Mystack<T> & Mystack<T>::operator=(const Mystack<T> &source) // assignment operator overload
{
input = new T[source.capacity];
top = source.top;
capacity = source.capacity;
for (int i = 0; i <= source.top; i++)
{
input[i] = source.input[i];
}
return *this;
}
Run Code Online (Sandbox Code Playgroud)
主要功能片段
Mystack <int> intstack …Run Code Online (Sandbox Code Playgroud) 我想知道当top变量达到-1时没有应用异常处理(没有元素留给pop).目前,我正在使用cout为用户提供有关堆栈下溢和返回0的信息,这不是一个好习惯.总体上可以对此pop函数进行哪些改进,以及如何在堆栈达到下溢状态时通知用户并处理异常.
int Mystack::pop()
{
if (isEmpty())
{
std::cout << "Stack Underflow" << std::endl;
}
else
{
std::cout << "The popped element is" << A[top];
return A[top--];
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
主要部分:
case 4:
std::cout << "POP the element" << std::endl;
s1.pop();
break;
Run Code Online (Sandbox Code Playgroud)