使用 C++ 中的函数返回 queue<vector>

Posted

技术标签:

【中文标题】使用 C++ 中的函数返回 queue<vector>【英文标题】:return queue<vector> using a function in c++ 【发布时间】:2014-09-06 16:52:27 【问题描述】: //16行 0000 0000 0001 1000 0010 0001 0011 1100 0100 0010 0101 0101 0110 1001 0111 1110 1000 0100 1001 0110 1010 1010 1011 0111 1100 0011 1101 1011 1110 1101 1111 1111

我创建了两个向量

std::vector&lt;std::bitset&lt;4&gt; &gt; a 第一列

std::vector&lt;std::bitset&lt;4&gt; &gt; b 第二列

我想用这些向量形成循环

示例:

a[0] == b[0] --> display it
a[1] != b[1] --> 
//queue function which returns queue
push a[1],b[1] into queue --> check the value of b[1] in `vector a`return the position of b[1] in     a --> push value in b at position return -- > repeat it until queue.back() == queue.front() -- return    the entire queue
//end of queue

a[0] == b[0] -- > display it
a[1] != b[1] --> 
q1 <- a[1] , q1 <- b1 --> q1 = a[1],b[1],....
// function begin
then check value of b[1] in `a` and return the position `8` --> q1<-b[8] --> q1a[1],b[1],b[8],...      
then check value of b[8] in `a and return the position `4` --> q1<-b[4] -->    q1(a[1],b[1],b[8],b[4],.
repeat it until last value of b[position] == q1.front(); 
return the entire queue q1--> a[1],b[1],b[8],b[4],b[2]

//函数结束

我想使用一个函数,它在执行完所有操作后返回整个队列。是否可以返回向量值队列。如果是这样,任何人都可以给我一个示例,说明如何使用上面给出的示例对其进行编码

if(a.at(0) == b.at(0))

   std:: cout << "a==b";

else

   //queue function starts here       
   //do some operations
   //return the queue

【问题讨论】:

【参考方案1】:

假设您使用的是 C++ 11:

std::vector<std::bitset<4>> getVector() 
    std::vector<std::bitset<4>> result;

    result.push_back( ... );

    return result;

或在 C++ 03 中使用引用:

void getVector(std::vector<std::bitset<4> >& result) 
    result.push_back( ... );

【讨论】:

以上是关于使用 C++ 中的函数返回 queue<vector>的主要内容,如果未能解决你的问题,请参考以下文章

c++ STL queue:deque+优先队列

C++ STL之queue详解

c++ queue 使用的问题

C++ 队列queue用法

使用 C++ 模板函数编译时间递归

从 C++ 中的函数返回指针