我在 Python 中使用出队。我知道要从出队中删除某个项目,我可以通过其索引来完成。像这样:
from collections import deque
deq = deque([1, 2, 3, 4, 5, 6, 7, 8])
del deq[1]
Run Code Online (Sandbox Code Playgroud)
现在我想从出队末尾删除多个项目,所以我使用这个:
from collections import deque
deq = deque([1, 2, 3, 4, 5, 6, 7, 8])
for i in range(1, 5):
del deq[-i]
print(deq)
Run Code Online (Sandbox Code Playgroud)
我期望新deq的是,deq=[1, 2, 3, 4]但输出是deq = [1, 3, 5, 7]。为什么会发生这种情况?
在 上调用 Python 的内置len()函数的时间复杂度是collections.deque多少?我希望它是 O(1),但我还没有找到对这个事实的任何确认。
我正在尝试使用VecDeque. 我想将它用作对所有线程具有读写权限的共享队列。我有以下代码:
use std::collections::VecDeque;
use std::{thread, time};
fn main() {
let mut workload = VecDeque::new();
workload.push_back(0);
let mut thread_1_queue = workload.clone();
let thread_1 = thread::spawn(move || {
let mut counter1: i32 = 0;
let some_time = time::Duration::from_millis(50);
loop {
counter1 +=1;
thread_1_queue.push_back(counter1);
println!("Thread #1: {:?}", thread_1_queue);
if counter1 == 10 {
break;
}
thread::sleep(some_time);
};
});
let mut thread_2_queue = workload.clone();
let thread_2 = thread::spawn(move || {
let mut counter2: i32 = 10;
let some_time = time::Duration::from_millis(50);
loop …Run Code Online (Sandbox Code Playgroud) 为什么这段代码工作正常:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
cell.textLabel.text = [NSString stringWithFormat:@"cell%i%i", indexPath.section, indexPath.row];
}
return cell;
}
Run Code Online (Sandbox Code Playgroud)
据我所知,单元标识符,只有当我移出cell.textLabel.text = ...if语句时,此代码才能正常工作.换句话说,为什么标签有正确的文本?
如何在Haskell中编写一个双端队列("deque").数据结构应该具有emptyDeque,front,back,removeFront,removeBack,addFront,addBack和isEmpty等函数,然后在 - >和< - 之间显示双端队列.
这是相同的,但对于一个队列:
module Queues (Queue, emptyQueue, front, remove, add, isEmpty)
newtype Queue a = Queue [a]
emptyQueue = Queue []
front (Queue (x:xs)) = x
front (Queue []) = error("No front of empty queue")
add (Queue xs) x = Queue (xs ++ [x])
remove (Queue (x:xs)) = Queue xs
remove (Queue []) = error("Nothing on queue to remove")
isEmpty (Queue []) = True
isEmpty (Queue (x:xs)) = False
showElems [] = ""
showElems (x:xs) = " …Run Code Online (Sandbox Code Playgroud) 在C++中,我所要做的就是
#include <queue> -> including
queue<int> a; -> defining
a.push(1); ->using
Run Code Online (Sandbox Code Playgroud)
但在java中我发现很难使用简单的deque我该怎么办...?更具体地说,我应该如何编写代码来简单地执行与在C++中相同的步骤; 包括,定义,使用.
更具体地说,我想制作一个双端队列,以便我可以在前面或后面的双端队列中添加任何整数.并根据双端队列的大小打印该双端队列中的整数
我应该编写一个类似于Unix tail函数的非常小的程序,我std::deque用来存储我std::getline从提供的文件中读取的行.从前面推,从后面弹出.
我的问题是,当我尝试打印更多行然后有文件时,它会在输出开始时输出1个额外的空白行.这里是源代码,TParams是struct与int lncount存储要求的行数,其中,和其他一些在当前并不重要的东西...
using namespace std;
deque<string> dq;
int counter = 0;
for(string line; ! (*infile).eof(); getline(*infile, line)){
dq.push_front(line);
// not needed lines dropped immediately
if(counter++ >= TParams.lncount)
dq.pop_back();
}
int iter = (TParams.lncount > dq.size()) ?
(dq.size() - 1) : (TParams.lncount - 1);
assert(iter < dq.size());
for(iter; iter >= 0; iter--)
cout << dq[iter] << endl;
Run Code Online (Sandbox Code Playgroud)
有一些关于-n +num参数的代码,但它是内部条件,并不影响这种情况.
我发现,实际上存在零长度字符串,dq.back()但我完全不知道它来自哪里,因为它应该是从文件开头读取的字符串,但是有正常的文本行.
目前在我的项目中我有两个静态方法PushObjects和ProcessObject.该PushObject方法将数据推送到静态双端队列,这个方法可以被多个线程访问,但是ProcessObject它总是由单个线程使用,用于从顶部检索对象并删除它们.现在我的问题是无论我尝试什么我总是最终(迟早会得到一个deque iterator not dereferencable错误.关于我能做些什么来阻止这个问题的任何建议.我的摘要PushObjects和ProcessObject下面给出的
void foo::PushObjects(obj A)
{
try
{
{//Begin Lock
boost::lock_guard<boost::mutex> lock(mutex_push);
mydeque.push_back(A);
}//End Lock
condition_read.notify_one(); //Inform the reader that it could start reading
}
catch (std::exception& e)
{
__debugbreak();
}
}
This is the static Reader method
void foo::ProcessObject()
{
{//Begin Lock
boost::unique_lock<boost::mutex> lock(mutex_process);
while(true)
{
while(mydeque.empty()) { condition_read.wait(lock); }
try
{
if(!mydeque.empty())
{
obj a = mydeque.front();
......Process the object........
mydeque.pop_front();
} …Run Code Online (Sandbox Code Playgroud) 有什么办法可以按索引删除双端队列中的项目?
例如
dq = deque(['a','b','c'])
dq.removeByIndex(1)
#output deque(['b', 'c'])
Run Code Online (Sandbox Code Playgroud)
我只在文档中看到按值删除。我也知道我可以把它弹出几次然后放回去,但是看起来并不漂亮。谢谢。
参考 https://docs.python.org/2/library/collections.html#collections.deque