Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/164.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 插入排序算法,我的代码怎么了?_C++_Sorting_Insertion Sort - Fatal编程技术网

C++ 插入排序算法,我的代码怎么了?

C++ 插入排序算法,我的代码怎么了?,c++,sorting,insertion-sort,C++,Sorting,Insertion Sort,我正在尝试一种InsertSort算法,用于输入存储在向量中的字符串 我要做的是在向量中输入一些字符串, 然后我使用insertionsort对向量进行排序 但我不知道为什么它不起作用!谁能给我指一下正确的方向吗 #include <iostream> #include <cstring> #include <vector> #include <cstdlib> using namespace std; int main (){ vect

我正在尝试一种InsertSort算法,用于输入存储在向量中的字符串

我要做的是在向量中输入一些字符串, 然后我使用insertionsort对向量进行排序

但我不知道为什么它不起作用!谁能给我指一下正确的方向吗

#include <iostream>
#include <cstring>
#include <vector>
#include <cstdlib>
using namespace std;

int main (){
    vector <string> names; //vector to store
    string input; //input is the variable of strings

    cout<<"Input a list of names \n";
    cout<<"To end the list type 'END'" <<endl;

    while (true){
        getline(cin, input);

        if (input =="END")
        break;

        names.push_back(input); //push into vector names
    }


  //my insertsort starts here
  string temp;
  int i;
  for (int j = 1; j < names.size(); j++){
        i = j - 1;        

        while ((i>0) && (names[i]>names[j]) ) {

            names[i+1] = names[i];

            i=i-1;         
                                                 }

        names[i+1] = names[j];

    }



    for (unsigned int i = 0; i<names.size (); i++)
    cout<<names[i]<<endl;
    cout<<endl;
    system("pause");
}
#包括
#包括
#包括
#包括
使用名称空间std;
int main(){
向量名称;//要存储的向量
字符串输入;//输入是字符串的变量
难道你错过了一点:

 //You have to remember names[j] before while loop
 //the variable temp is never used
 temp = names[j];
 while ((i>=0) && (names[i]>temp) ) {
    names[i+1] = names[i];
    i=i-1;                                                  
 }
 names[i+1] = temp;
 // since names[j] was already been filled by other words during swapping

如果不需要使用插入排序,最好使用stl排序算法。

定义“不工作”.您使用的是什么输入?您得到的是什么输出?谢谢,我按照您的建议编辑了代码,但输出仍然错误,我也不知道。我缺少什么吗?@CandyMan I>0应该是I>=0,否则第一个点将不会被检查。看到我更新的答案吗?啊!非常感谢,tacp!对不起,但我认为输出是错误的长输入错误。我的算法怎么了?啊,我的意思是,当我输入更多的元素,而不是苹果,糖果,鸡蛋,我有,苹果,糖果,鸡蛋,黄油,彼得。
 //You have to remember names[j] before while loop
 //the variable temp is never used
 temp = names[j];
 while ((i>=0) && (names[i]>temp) ) {
    names[i+1] = names[i];
    i=i-1;                                                  
 }
 names[i+1] = temp;
 // since names[j] was already been filled by other words during swapping