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)
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>> = {<__gn开发者_开发百科u_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.
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::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.
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() );
}
精彩评论