简体   繁体   English

如何在不使用标准算法的情况下将c元素添加到排序向量中?

[英]How to add c element in sorted vector without using standard algorithms?

My code does not work in some cases: when c1 is 4 , it doesn't output anything, but it works for numbers greater than 9. Why is that? 我的代码在某些情况下不起作用:当c14 ,它不输出任何内容,但对大于9的数字有效。为什么?

#include<iostream>
#include<vector>
using namespace std;
void insertfast(vector<int>&v, int c)
{
if (c >= v[v.size()-1])v.push_back(c);
if (c <= v[0])v.insert(v.begin(), c);
int min = 1;
int max = v.size();
while (v.size() != 9) {
    int i = (min + max) / 2;
    if (v[i - 1] <= c && c <= v[i])v.insert(v.begin() + i, c);
    if (v[i] <= c && c <= v[i + 1])v.insert(v.begin() + (i + 1), c);
    if (c < v[i])
        max = i;
    else
        min = i;
}
}
int main()
{
vector<int>v1 = { 2,5,9,22,44,55,88,777 };
int c1 = 4;

insertfast(v1, c1);

for (int i = 0; i < 9; i++)
    cout << v1[i] << endl;
}

You have two logic problems in your code: 您的代码中存在两个逻辑问题:

  1. For your test data in insertfast() function you step into while() , when you step into first if (v[i - 1] <= c && c <= v[i]) you insert 4 value and in becomes v[1] , and you have step right after that into next if (v[i] <= c && c <= v[i + 1]) , so you need to set second if as else if , like this: 对于insertfast()函数中的测试数据,您进入while() ,当您首先进入if (v[i - 1] <= c && c <= v[i])您插入4值,而in成为v[1] ,然后紧接着进入if (v[i] <= c && c <= v[i + 1])下一个步,因此您需要将if设置为else if ,例如:

     if (v[i - 1] <= c && c <= v[i]) { v.insert(v.begin() + i, c); } else if (v[i] <= c && c <= v[i + 1]) { v.insert(v.begin() + (i + 1), c); } 
  2. From the previous item your vector increased for 2 elements, so his size becomes 10 , and you had infinite loop while (v.size() != 9) , I think you can just do break to leave loop in case of success insert: 从上一项开始,您的向量增加了2个元素,因此他的大小变为10while (v.size() != 9)出现了无限循环,如果成功插入,我认为可以break循环:

     if (v[i - 1] <= c && c <= v[i]) { v.insert(v.begin() + i, c); break; } else if (v[i] <= c && c <= v[i + 1]) { v.insert(v.begin() + (i + 1), c); break; } 

There is no need to run loop further, if you have inserted element. 如果插入了元素,则无需进一步运行循环。 Actually 2nd item with break will fix and else missing problem, in case of insertion it will leave loop and won't be able to step into second if. 实际上,带有break第二项将解决, else丢失问题,如果插入,它将离开循环,并且如果进入第二项将无法进入。

Let's just take a second to logically step through the code here, since I assume this is a homework question, it's important to understand how to do this. 让我们花点时间从逻辑上逐步看一下这里的代码,因为我认为这是一个家庭作业问题,因此了解如何执行此操作很重要。 There are tools like debuggers that are used on larger projects and more difficult problems, but small logic errors like this happen all the time. 有一些像调试器之类的工具可用于较大的项目和更困难的问题,但是像这样的小逻辑错误始终会发生。

Ok, so insertfast() is a function to insert an int into a vector<int> in an ordered manner. 好的,所以insertfast()是一个将int按有序方式插入到vector<int>中的函数。 The first thing you do is compare against the first and last elements of the vector. 您要做的第一件事是与向量的第一个和最后一个元素进行比较。

if (c >= v[v.size() - 1]) 
    v.push_back(c); 
if (c <= v[0]) 
    v.insert(v.begin(), c);

We can only be one of these things, and if either are true we are done. 我们只能是其中的一种,如果任何一种都成立,那么我们就完成了。 So the best option here is to just return from the function, we don't care about doing anything else. 因此,这里最好的选择是仅从函数return ,我们不在乎做任何其他事情。 We know that, but the program doesn't. 我们知道,但是程序不知道。 It'll keep on comparing things we don't want to compare and bug out. 它将继续比较我们不想比较的东西并进行调试。

if (c >= v[v.size() - 1]) {
    v.push_back(c);
    return;
}
if (c <= v[0]) {
    v.insert(v.begin(), c);
    return;
}

What if both of these statements are false, and we are still in the function? 如果这两个语句都为假,而我们仍在函数中怎么办? We still need to figure where to insert this value, so we loop through the elements. 我们仍然需要弄清楚该值的插入位置,因此我们遍历元素。

while (v.size() != 9)

This is a very poor conditional loop, since it will only work if we pass a vector of size 8 into the function. 这是一个非常差的条件循环,因为只有将大小为8的向量传递给函数时,它起作用。 With your particular arithmetic, you essentially want to loop until some value is added to the vector. 使用您自己的特定算法,您本质上想循环直到向向量添加一些值。 I recommend an unconditional loop (we are trying to be "fast" after all) aka an infinite loop, and explicitly return or break within the loop. 我建议一个无条件循环(毕竟我们试图做到“快速”)又是一个无限循环,并明确地在循环内返回或中断。

int min = 1;
int max = v.size();
while (1) {
    int i = (min + max) / 2;
    if (v[i - 1] <= c && c <= v[i]) {
        v.insert(v.begin() + i, c);
        return;
    }
    if (v[i] <= c && c <= v[i + 1]) {
        v.insert(v.begin() + (i + 1), c);
        return;
    }
    if (c < v[i])
        max = i;
    else
        min = i;
}

break instead of return will also work just fine here, since if you break out of this loop, the end of the function is hit and you just return anyways. 在这里, break而不是return也可以正常工作,因为如果您退出此循环,函数的末尾将被命中,并且无论如何您都将返回。 So, we solved the problem just by rethinking the design and stepping through the code. 因此,我们仅需重新考虑设计并逐步执行代码即可解决该问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM