[英]unable to push items in vector and list in C++?
This is a basic merge sort program:这是一个基本的归并排序程序:
The problem is when I try to push item in to "vector merged", nothing happens.问题是当我尝试将项目推入“矢量合并”时,没有任何反应。 (Please refer the gdb data below the code)
(请参考代码下方的gdb数据)
vector<long> merge(vector<long> &L, vector<long> &R){
int i = 0;
int j = 0;
vector<long> merged;
while((i < L.size())&&(j < R.size())){
if ((L[i] <= R[j])){
merged.push_back(L[i]);
i++;
}else{
merged.push_back(R[j]);
j++;
}
while(i < L.size())
{
merged.push_back(L[i]);
i++;
}
while(j < R.size())
{
merged.push_back(R[j]);
j++;
}
return merged;
}
}
//===================================GDB DATA=================================== //====================================GDB DATA=========== =========================
(gdb) n
52 int i = 0;
(gdb)
53 int j = 0;
(gdb)
54 vector<long> merged;
(gdb) print i
$2 = 0
(gdb) print merged
$3 = {<std::_Vector_base<long, std::allocator<long> >> = {
_M_impl = {<std::allocator<long>> = {<__gnu_cxx::new_allocator<long>> = {<No data fields>}, <No data fields>}, _M_start = 0x7fffffffdcd0,
_M_finish = 0x7fffffffdcf0, _M_end_of_storage = 0x7fffffffdde0}}, <No data fields>}
(gdb) print *(merged._M_impl._M_start)@merged.size()
$4 = {140737488346528, 4199556, 140737488346432, 140737488346656}
Problem I: Why the merged vector is not null? Or did I used wrong gdb command?? But same command works on other vectors in this program..
(gdb)
$5 = {140737488346528, 4199556, 140737488346432, 140737488346656}
(gdb) n
56 while((i < L.size())&&(j < R.size())){
(gdb) print *(merged._M_impl._M_start)@merged.size()
$6 = {140737488346528, 4199556, 140737488346432, 140737488346656}
(gdb) n
57 if ((L[i] <= R[j])){
(gdb) print L[i]
$7 = (long &) @0x6060b0: 31616136
(gdb) print R[i]
$8 = (long &) @0x6060d0: 1873051691
(gdb) n
58 merged.push_back(L[i]);
(gdb) print *(merged._M_impl._M_start)@6
$14 = {140737488346528, 4199556, 140737488346432, 140737488346656, 6316272, 6316280}
// Before any push_back operation, dump the content of merged.
(gdb) n
59 i++;
// Question 2, Push_back does not work...
(gdb) print *(merged._M_impl._M_start)@1
$17 = {140737488346528}
(gdb) print *(merged._M_impl._M_start)@2
$18 = {140737488346528, 4199556}
(gdb) print *(merged._M_impl._M_start)@8
$19 = {140737488346528, 4199556, 140737488346432, 140737488346656, 6316272, 6316280, 6316280, 4205125}
Could anyone give me a hand??谁能帮我一把??
You have a bug in the algorithm, right now it seems like the first item will be merged correctly, but then all of L will be pushed and after that all of R.您在算法中有一个错误,现在似乎第一项将被正确合并,但随后所有 L 将被推送,然后所有 R 将被推送。
It seems that moving a closing brace will solve your problem.似乎移动右大括号可以解决您的问题。
vector<long> merge(vector<long> &L, vector<long> &R){
int i = 0;
int j = 0;
vector<long> merged;
while((i < L.size())&&(j < R.size())){
if ((L[i] <= R[j])){
merged.push_back(L[i]);
i++;
}else{
merged.push_back(R[j]);
j++;
}
}
while(i < L.size())
{
merged.push_back(L[i]);
i++;
}
while(j < R.size())
{
merged.push_back(R[j]);
j++;
}
return merged;
}
See std::merge见std::merge
std::vector<long> merge(std::vector<long>& lhs, std::vector<long>& rhs)
{
std::vector<long> result(rhs.size() + lhs.size());
std::merge(lhs.begin(), lhs.end(), rhs.begin(), rhs.end(), result.begin());
return result;
}
While I acknowledge the benefits of merging the two vectors together, this code also works by copying the two vectors together and then sorting.虽然我承认将两个向量合并在一起的好处,但这段代码也可以通过将两个向量复制在一起然后排序来工作。 I also removed a potential copy of the entire merged vector upon return, by passing in the output vector as a parameter.
我还在返回时删除了整个合并向量的潜在副本,方法是将 output 向量作为参数传递。
void merge(const vector<long>& lhs, const vector<long>& rhs, vector<long>& merged ) {
merged.resize(lhs.size()+rhs.size());
copy( lhs.begin(), lhs.end(), merged.begin() );
copy( rhs.begin(), rhs.end(), merged.begin() + lhs.size() );
sort( merged.begin(), merged.end() );
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.