有没有办法从一长串数字的开头删除元素?现在我正在执行 del arr[i:i+x] 但它很慢,因为它必须将所有经过该点的内容移到左侧,这对于大型列表来说非常耗时。
我研究了双端队列,但不确定它们是否适用于这里。可以使用一些方向!
我有一个完整的 Deque Array 类,如下所示:
from collections import deque
import ctypes
class dequeArray:
DEFAULT_CAPACITY = 10 #moderate capacity for all new queues
def __init__(self):
self.capacity = 5
capacity = self.capacity
self._data = self._make_array(self.capacity)
self._size = 0
self._front = 0
def __len__(self):
return self._size
def __getitem__(self, k): #Return element at index k
if not 0 <= k < self._size:
raise IndexError('invalid index')
return self._data[k]
def isEmpty(self):
if self._data == 0:
return False
else:
return True
def append(self, item): #add an element to …Run Code Online (Sandbox Code Playgroud) 所以我最近介绍了双端队列,我想知道是否有任何方法可以访问 acapacity()的 a std::deque,就像我们在 a 上所做的那样std::vector?
我发现这个成员函数std::deque::max_size但这代表:
双端队列容器可以作为内容容纳的元素的最大数量。
并且不符合我想要的。
有任何想法吗?
我必须在a上编写一小段代码deque,但是我不知道如何编写方法的代码,如果有人可以帮助我使用其中一种方法,(例如添加对象的方法)然后那将让我开始.来自deque)我确信我可以管理其余的方法,就在我很难过的那一刻.
我究竟做错了什么?
#include <iostream>
#include <deque>
using namespace std;
struct mystruct {
int number1;
int number2;
};
int main() {
std::deque<mystruct> mydeque;
mydeque.number1.push_front(77);
return 0;
}
Run Code Online (Sandbox Code Playgroud) 可以说我有一个结构
struct s
{
std::deque<Object> q; //won't work with C library
}
Run Code Online (Sandbox Code Playgroud)
如果使用C库初始化带有std :: deque的结构,那么它将无法工作.
struct s
{
std::vector<Object> v; //would work with C library
}
Run Code Online (Sandbox Code Playgroud)
但是,这个带有std :: vector的结构可以与C库一起使用.我认为这是因为双端队列中的元素不是连续的,而向量中的元素是连续的.我认为这可能是一个原因,但不确定.
我试图弄清楚如何在两个值上排序结构的双端队列,而不只是一个.我所拥有的代码就是我所拥有的完美排序arrival,但是如果两个项目相同pid,我希望它们也是pid顺序.我希望我有意义!
例如:
具有pid1和arrival10的a的进程应该在具有pid2和arrival10 的进程之前,即使具有pid1 的进程最初在deque中出现.
struct Process{
int pid;
int burst;
int arrival;
};
int sortOnArrival (Process const &a, Process const &b){
return a.arrival < b.arrival;
}
int main(int argc, char *argv[]){
deque<Process> readyQueue;
// This is just pseudocode, but trust me, it works. :)
fill(readyQueue);
sort(readyQueue.begin(), readyQueue.end(), sortOnArrival);
}
Run Code Online (Sandbox Code Playgroud) 给出以下代码:
void World::extractStates(deque<string> myDeque)
{
unsigned int i = 0;
string current; // current extracted string
while (i < myDeque.size()) // run on the entire vector and extract all the elements
{
current = myDeque.pop_front(); // doesn't work
// do more stuff
}
}
Run Code Online (Sandbox Code Playgroud)
我想在每个迭代中提取前面的元素,但这pop_front()是一个void
方法.我怎样才能获得元素(在前面)呢?
问候
我正在学习Java,并实现Deque数据结构.这是Node类:
import java.util.*;
public class Deque<Item> implements Iterable<Item> {
private Node sentinel;
private class Node {
Item item;
Node next;
Node previous;
Node(Item value) {
item = value;
next = this;
previous = this;
}
}
public Deque(Item item) // construct an empty deque
{
Node sentinel = new Node(item);
}
public boolean isEmpty() // is the deque empty?
{
return (size() == 0);
}
public int size() // return the number of items on the deque
{
System.out.println("size");
if …Run Code Online (Sandbox Code Playgroud) #include <iostream>
#include <string>
#include <deque>
#include <vector>
#include <unistd.h>
using namespace std;
struct Node
{
string str;
vector<string> vec;
Node(){};
~Node(){};
};
int main ()
{
deque<Node> deq;
for(int i = 0; i < 100; ++i)
{
Node tmp;
tmp.vec.resize(100000);
deq.push_back(tmp);
}
while(!deq.empty())
{
deq.pop_front();
}
{
deque<Node>().swap(deq);
}
cout<<"releas\n";
sleep(80000000);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
通过top,我发现我的程序的内存大约是61M,为什么?如果有一个复制构造函数就可以了Node.我想知道原因,而不是如何使它正确.
gcc(GCC)4.9.1,centos