我正在学习一个 C++ 数据结构类,我正在研究的问题是编写一个客户端函数来获取队列的长度,而无需使用函数原型(prototype)更改队列:
int GetLength(QueType queue);
在我看来,这非常简单,因为当您将队列对象传递给一个函数时,它正在使用一个拷贝,所以如果我迭代并出列直到它为空,我就知道队列中有多少项。
QueType是文中提供的简单队列类型,ItemType定义为
typedef char ItemType;
我的简单驱动程序代码如下:
#include "QueType.h"
using namespace std;
int GetLength(QueType queue);
int main()
{
ItemType item; //typedef char
//initialize and fill the queue
QueType que(5);
que.Enqueue('A');
que.Enqueue('B');
que.Enqueue('C');
que.Enqueue('D');
que.Enqueue('E');
cout << "The length of the queue is " << GetLength(que) << endl;
while (!que.IsEmpty())
{
que.Dequeue(item);
cout << "Dequeue item of queue: " << item << endl;
}
system("PAUSE");
return EXIT_SUCCESS;
}
int GetLength(QueType queue)
{
int cnt = 0;
ItemType item;
while (!queue.IsEmpty())
{
queue.Dequeue(item);
cout << "Dequeue item of local copy of queue: " << item << endl;
cnt++;
}
return cnt;
}
我的预期输出是:
Dequeue item of local copy of queue: A
Dequeue item of local copy of queue: B
Dequeue item of local copy of queue: C
Dequeue item of local copy of queue: D
Dequeue item of local copy of queue: E
The length of the queue is 5
Dequeue item of queue: A
Dequeue item of queue: B
Dequeue item of queue: C
Dequeue item of queue: D
Dequeue item of queue: E
Press any key to continue . . .
但是我得到了这个:
Dequeue item of local copy of queue: A
Dequeue item of local copy of queue: B
Dequeue item of local copy of queue: C
Dequeue item of local copy of queue: D
Dequeue item of local copy of queue: E
The length of the queue is 5
Dequeue item of queue: p
Dequeue item of queue: ↨
Dequeue item of queue: 7
Dequeue item of queue:
Dequeue item of queue: ─
Press any key to continue . . .
QueType.h: 类 FullQueue {};
class EmptyQueue
{};
typedef char ItemType;
class QueType
{
public:
QueType();
QueType(int max);
~QueType();
void MakeEmpty();
bool IsEmpty() const;
bool IsFull() const;
void Enqueue(ItemType newItem);
void Dequeue(ItemType& item);
private:
int front;
int rear;
ItemType* items;
int maxQue;
};
QueType.cpp:
#include "QueType.h"
QueType::QueType(int max)
{
maxQue = max + 1;
front = maxQue - 1;
rear = maxQue - 1;
items = new ItemType[maxQue];
}
QueType::QueType() // Default class constructor
{
maxQue = 501;
front = maxQue - 1;
rear = maxQue - 1;
items = new ItemType[maxQue];
}
QueType::~QueType() // Class destructor
{
delete [] items;
}
void QueType::MakeEmpty()
{
front = maxQue - 1;
rear = maxQue - 1;
}
bool QueType::IsEmpty() const
{
return (rear == front);
}
bool QueType::IsFull() const
{
return ((rear + 1) % maxQue == front);
}
void QueType::Enqueue(ItemType newItem)
{
if (IsFull())
throw FullQueue();
else
{
rear = (rear +1) % maxQue;
items[rear] = newItem;
}
}
void QueType::Dequeue(ItemType& item)
{
if (IsEmpty())
throw EmptyQueue();
else
{
front = (front + 1) % maxQue;
item = items[front];
}
}
代码获取长度但显然队列已被修改。如果该对象是通过引用传递的,则队列将为空,但事实并非如此,它只是在每个队列位置都有垃圾。有一些我不理解的概念。任何帮助将不胜感激!
最佳答案
重写入队和出队函数来递增和递减计数器怎么样...然后你就会知道队列中有多少项目。
关于c++ - 获取简单队列的长度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20873110/
在我的Rails(2.3,Ruby1.8.7)应用程序中,我需要将字符串截断到一定长度。该字符串是unicode,在控制台中运行测试时,例如'א'.length,我意识到返回了双倍长度。我想要一个与编码无关的长度,以便对unicode字符串或latin1编码字符串进行相同的截断。我已经了解了Ruby的大部分unicode资料,但仍然有些一头雾水。应该如何解决这个问题? 最佳答案 Rails有一个返回多字节字符的mb_chars方法。试试unicode_string.mb_chars.slice(0,50)
我试图获取一个长度在1到10之间的字符串,并输出将字符串分解为大小为1、2或3的连续子字符串的所有可能方式。例如:输入:123456将整数分割成单个字符,然后继续查找组合。该代码将返回以下所有数组。[1,2,3,4,5,6][12,3,4,5,6][1,23,4,5,6][1,2,34,5,6][1,2,3,45,6][1,2,3,4,56][12,34,5,6][12,3,45,6][12,3,4,56][1,23,45,6][1,2,34,56][1,23,4,56][12,34,56][123,4,5,6][1,234,5,6][1,2,345,6][1,2,3,456][123
我的瘦服务器配置了nginx,我的ROR应用程序正在它们上运行。在我发布代码更新时运行thinrestart会给我的应用程序带来一些停机时间。我试图弄清楚如何优雅地重启正在运行的Thin实例,但找不到好的解决方案。有没有人能做到这一点? 最佳答案 #Restartjustthethinserverdescribedbythatconfigsudothin-C/etc/thin/mysite.ymlrestartNginx将继续运行并代理请求。如果您将Nginx设置为使用多个上游服务器,例如server{listen80;server
有没有办法在这个简单的get方法中添加超时选项?我正在使用法拉第3.3。Faraday.get(url)四处寻找,我只能先发起连接后应用超时选项,然后应用超时选项。或者有什么简单的方法?这就是我现在正在做的:conn=Faraday.newresponse=conn.getdo|req|req.urlurlreq.options.timeout=2#2secondsend 最佳答案 试试这个:conn=Faraday.newdo|conn|conn.options.timeout=20endresponse=conn.get(url
我有一个驼峰式字符串,例如:JustAString。我想按照以下规则形成长度为4的字符串:抓取所有大写字母;如果超过4个大写字母,只保留前4个;如果少于4个大写字母,则将最后大写字母后的字母大写并添加字母,直到长度变为4。以下是可能发生的3种情况:ThisIsMyString将产生TIMS(大写字母);ThisIsOneVeryLongString将产生TIOV(前4个大写字母);MyString将生成MSTR(大写字母+tr大写)。我设法用这个片段解决了前两种情况:str.scan(/[A-Z]/).first(4).join但是,我不太确定如何最好地修改上面的代码片段以处理最后一种
我有一个存储主机名的Ruby数组server_names。如果我打印出来,它看起来像这样:["hostname.abc.com","hostname2.abc.com","hostname3.abc.com"]相当标准。我想要做的是获取这些服务器的IP(可能将它们存储在另一个变量中)。看起来IPSocket类可以做到这一点,但我不确定如何使用IPSocket类遍历它。如果它只是尝试像这样打印出IP:server_names.eachdo|name|IPSocket::getaddress(name)pnameend它提示我没有提供服务器名称。这是语法问题还是我没有正确使用类?输出:ge
我想获取模块中定义的所有常量的值:moduleLettersA='apple'.freezeB='boy'.freezeendconstants给了我常量的名字:Letters.constants(false)#=>[:A,:B]如何获取它们的值的数组,即["apple","boy"]? 最佳答案 为了做到这一点,请使用mapLetters.constants(false).map&Letters.method(:const_get)这将返回["a","b"]第二种方式:Letters.constants(false).map{|c
我安装了ruby版本管理器,并将RVM安装的ruby实现设置为默认值,这样'哪个ruby'显示'~/.rvm/ruby-1.8.6-p383/bin/ruby'但是当我在emacs中打开inf-ruby缓冲区时,它使用安装在/usr/bin中的ruby。有没有办法让emacs像shell一样尊重ruby的路径?谢谢! 最佳答案 我创建了一个emacs扩展来将rvm集成到emacs中。如果您有兴趣,可以在这里获取:http://github.com/senny/rvm.el
我想在Ruby中创建一个用于开发目的的极其简单的Web服务器(不,不想使用现成的解决方案)。代码如下:#!/usr/bin/rubyrequire'socket'server=TCPServer.new('127.0.0.1',8080)whileconnection=server.acceptheaders=[]length=0whileline=connection.getsheaders想法是从命令行运行这个脚本,提供另一个脚本,它将在其标准输入上获取请求,并在其标准输出上返回完整的响应。到目前为止一切顺利,但事实证明这真的很脆弱,因为它在第二个请求上中断并出现错误:/usr/b
假设我有这个范围:("aaaaa".."zzzzz")如何在不事先/每次生成整个项目的情况下从范围中获取第N个项目? 最佳答案 一种快速简便的方法:("aaaaa".."zzzzz").first(42).last#==>"aaabp"如果出于某种原因你不得不一遍又一遍地这样做,或者如果你需要避免为前N个元素构建中间数组,你可以这样写:moduleEnumerabledefskip(n)returnto_enum:skip,nunlessblock_given?each_with_indexdo|item,index|yieldit