我一直习惯cout打印声明,但现在我想学习打印passing the stream,就像 void print(std::ostream&) const;我目前的打印功能一样
template
void Mystack::print()
{
for (int i = 0; i <= top; i++)
{
std::cout <
我有两个问题:
从我上面实现的普通打印功能切换到使用的打印功能有什么好处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 should implement Rule of three.
//Concepts : Shallow Copy and Deep Copy
#include
template
class Mystack
{
private:
T *input;
int top;
int capacity;
public:
Mystack();
~Mystack();
void push(T const& x);
void pop();
T& topElement() const;
bool isEmpty() const;
void print();
};
template
Mystack::Mystack()
{
top = -1;
capacity = 5;
input = new T[capacity];
}
template
Mystack::~Mystack() //Since we are using destructor explictly we need to apply Rule of 3
{
delete [] input;
}
template
void Mystack::push(T const& x) //Passing x by Const Reference // Valus of x cannot be changed now in the function!
{
if (top + 1 == capacity)
{
T *vec = new T[capacity * 2];
for (int i = 0; i <= top; i++)
{
vec[i] = input[i];
}
delete []input; // Avoiding Memory Leak.
input = vec;
capacity *= capacity;
}
top++;
std::cout <
void Mystack::pop()
{
if (isEmpty())
{
throw std::out_of_range("Stack Underflow");
}
else
{
std::cout <<"The popped element is" <
bool Mystack::isEmpty() const
{
if (top == -1)
{
std::cout <<"Is Empty" <
T& Mystack::topElement() const
{
if (top == -1)
{
throw std::out_of_range("No Element to Display");
}
else
{
std::cout <<"The top element is : " <
void Mystack::print()
{
for (int i = 0; i <= top; i++)
{
std::cout < s1;
Mystack s2;
Mystack s3;
int choice;
int int_elem;
float float_elem;
char char_elem;
std::cout <<"Enter the type of stack" <> choice;
if (choice == 1)
{
int ch = 1;
while (ch > 0)
{
std::cout <<"\n1. Push ";
std::cout <<"2. Top ";
std::cout <<"3. IsEmpty ";
std::cout <<"4. Pop ";
std::cout <<"5. Exit ";
std::cout <<"6. Print"<> ch;
switch (ch)
{
case 1:
std::cout <<"Enter the number to be pushed" <> int_elem;
s1.push(int_elem);
break;
case 2:
std::cout <<"Get the TOP Element" < 0)
{
std::cout <<"\n1. PUSH" <> ch;
switch (ch)
{
case 1:
std::cout <<"Enter the number to be pushed" <> float_elem;
s2.push(float_elem);
break;
case 2:
std::cout <<"Get the TOP Element" < 0)
{
std::cout <<"\n1. PUSH" <> ch;
switch (ch)
{
case 1:
std::cout <<"Enter the number to be pushed" <> char_elem;
s3.push(char_elem);
break;
case 2:
std::cout <<"Get the TOP Element" <
MondKin..
7
The benefit of switching to the ostream version is that in the case you later need to print to other places besides std::cout then you can do it with the same function implementation, whereas in this moment if you want to print to a file you would need to use a different function.
An example of how to implement it is instead of doing this:
void print()
{
std::cout <<"Print something always to cout" <
You do this (notice we are passing a reference):
void print(std::ostream& os)
{
os <<"Print something to wherever the caller wants to" <
The benefit of switching to the ostream version is that in the case you later need to print to other places besides std::cout then you can do it with the same function implementation, whereas in this moment if you want to print to a file you would need to use a different function.
An example of how to implement it is instead of doing this:
void print()
{
std::cout <<"Print something always to cout" <
You do this (notice we are passing a reference):
void print(std::ostream& os)
{
os <<"Print something to wherever the caller wants to" <
在视频传输领域,MP4虽然常见,但在直播场景中直接使用MP4格式存在诸多问题。例如,MP4文件的头部信息(如ftyp、moov)较大,导致初始加载时间较长,影响用户体验。相比之下,HLS(HTTP Live Streaming)协议及其M3U8格式更具优势。HLS通过将视频切分成多个小片段,并生成一个M3U8播放列表文件,实现低延迟和高稳定性。本文详细介绍了如何将TS文件转换为M3U8直播流,包括技术原理和具体操作步骤,帮助读者更好地理解和应用这一技术。 ...
[详细]