【发布时间】:2019-12-10 10:44:27
【问题描述】:
如何将一个队列的值(不是它引用)排入另一个队列? 它的工作方式就像我在 C++ 中有一个点队列(Queue),但我想像这样复制缓冲区队列的值
a = 1;
int[] array = new int[1]
array[0] = a //array[0] now is 1
a = 0 // but array[0] doesn't change, array[0] is 1!
words.Enqueue(buffer) 有问题
using word = System.Collections.Generic.Queue<char>;
Queue<word> words = new Queue<word>(); //word is the custom type, that was def in file top
word buffer = new word();
for (var symbol_count = 0; symbol_count < text.Length; ++symbol_count)
{
if (text[symbol_count] != ' ' && text[symbol_count] != '.' && text[symbol_count] != ',')
{
buffer.Enqueue(text[symbol_count]); //store one char in word
}
else
{
buffer.Enqueue(text[symbol_count]); //store end of word symbol
words.Enqueue(buffer); //store one word in words queue, but compiler do it like I try to copy a reference of buffer, not it value!!!
//System.Console.WriteLine(words.Count); DEBUG
buffer.Clear(); //clear buffer and when i do this, value in words queue is deleted too!!!
}
}
【问题讨论】:
-
“我的 word.Enqueue(buffer) 有问题”。那么问题出在哪里?
-
我在“单词”队列中插入的不是“缓冲区”队列的值,而是指针。当我更改“缓冲区”队列的值时,它也会在“单词”队列中更改。但我只想在“单词”队列中复制“缓冲区”队列的值
-
当你使用别名
Queue<char>时读起来有点混乱 -
stackoverflow.com/questions/16209747/cloning-queue-in-c-sharp 的副本。或者只是创建一个新队列而不是
Clearing 旧队列。 -
我不确定我是否完全理解您的问题,但听起来您想在队列中制作一个值的深层副本并将其排入另一个队列?这是一个可能有帮助的链接:stackoverflow.com/questions/129389/…