这是一个基本的合并排序程序:
问题是当我尝试将项目推入“向量合并”时,没有任何反应。 (请参阅代码下方的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) 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}
有人能帮我一把吗?
答案 0 :(得分:4)
你在算法中有一个错误,现在似乎第一个项目将被正确合并,但随后所有的L将被推送,之后全部为R。
似乎移动一个右大括号可以解决你的问题。
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;
}
答案 1 :(得分:0)
请参阅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;
}
答案 2 :(得分:-1)
虽然我承认将两个向量合并在一起的好处,但这个代码也可以通过将两个向量复制在一起然后进行排序来实现。我还通过将输出向量作为参数传递,在返回时删除了整个合并向量的潜在副本。
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() );
}