Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/152.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 - Fatal编程技术网

C++ 选择排序是如何工作的?

C++ 选择排序是如何工作的?,c++,sorting,C++,Sorting,这是一种我正在尝试的选择类型,我不明白为什么它不起作用。我的理解是,选择排序会扫描一个向量,寻找最小的值,当它找到这个值时,会将它移到向量的开头。这一次,它将执行另一次扫描,忽略第一个元素,然后重新进行扫描,直到n-1倍,其中n是向量的长度 #include <iostream> #include <string> #include <iomanip> #include <vector> #include <algorithm> #in

这是一种我正在尝试的选择类型,我不明白为什么它不起作用。我的理解是,选择排序会扫描一个向量,寻找最小的值,当它找到这个值时,会将它移到向量的开头。这一次,它将执行另一次扫描,忽略第一个元素,然后重新进行扫描,直到n-1倍,其中n是向量的长度

#include <iostream>
#include <string>
#include <iomanip>
#include <vector>
#include <algorithm>
#include <cstdlib>
#include <map>
using namespace std;





int main()
{
    vector <string> vName, vID, vClass;
    string sName, minValue, sID, sClass, sSearch, sQuestion, ssSearch, sSearchN, sSearchI;
    int iSize, iStudent;
    // Display initial vector size
    iSize = vName.size();

    cout << "Student list starts with the size:" << iSize << endl;

    // Get size of list from user

    cout << "How many students would you like to add?" << endl;
    cin >> iStudent;
    cin.ignore();
    // Get names, ids, and classes

    for (int i = 0; i < iStudent; i++)
    {
        cout << "Student" << i + 1 << ":\n";
        cout << "Please enter the student name: ";
        getline(cin, sName);
        vName.push_back(sName);

        cout << "Enter ID number ";
        getline(cin, sID);
        vID.push_back(sID);

        cout << "Enter class name ";
        getline(cin, sClass);
        vClass.push_back(sClass);

    }
    // Display header

    cout << "The list of students has the size of: " << iStudent << endl;
    cout << "The Student List" << endl;
    cout << "\n";
    cout << "Name:" << setw(30) << "ID:" << setw(38) << "Enrolled Class : " << endl;
    cout << "--------------------------------------------------------------------------";
    cout << "\n";

    // for loop for displying list
    for (int x = 0; x < vName.size() && vID.size() && vClass.size(); x++)
    {

        cout << vName[x] << "\t \t \t" << vID[x] << "\t \t \t" << vClass[x] << endl;
    }

    // Sorting function and for loop to display sorted names
    cout << "\n";
    cout << "The Student List after Sorting:" << endl;
    cout << "\n";


    //*************************************
    int startScan, minIndex;


    for (startScan = 0; startScan < vName.size() - 1; startScan++)
    {
        minIndex = startScan;
        minValue = vName[startScan];
        for (int index = startScan + 1; index < vName.size(); index++)
        {
            if (vName[index] < minValue)
            {
                minValue = vName[index];
                minIndex = index;
            }
        }
        vName[minIndex] = vName[startScan];
        vName[startScan] = minValue;
        vID[minIndex] = vID[startScan];
        vID[startScan] = minValue;
        vClass[minIndex] = vClass[startScan];
        vClass[startScan] = minValue;

    }


    //******************


    //sort(vName.begin(), vName.end());

    //for (int y = 0; y < vName.size(); y++)
    //{
    //  cout << vName[y] << endl;
    //}

    cout << "\n";

    // Search function uses a do while loop that loops so long as the user inputs a "y" or "Y"


    do
    {
        int iPick;
        cout << "Search Menu:" << endl;
        cout << "1. By Name\n";
        cout << "2. By ID\n \n";
        cin >> iPick;
        if (iPick == 1)
        {
            cout << "Please Enter a name to be searched:" << endl;
            getline(cin >> ws, sSearchN);
            if (binary_search(vName.begin(), vName.end(), sSearchN))
            {



                cout << sSearchN << " was found." << endl << endl;
            }


            else
            {

                cout << sSearchN << " was not found." << endl << endl;


            }

            cout << "Would you like to search another name?" << endl << endl;
            cout << vName[0];
            cout << "Please enter Y for Yes and N for No:" << endl << endl;
            getline(cin >> ws, sQuestion);
        }
        else
        {
            cout << "Please Enter an ID to be searched:" << endl;
            getline(cin >> ws, sSearchI);
            if (binary_search(vID.begin(), vID.end(), sSearchI))
            {
                cout << sSearchI << " " << "was found"  << endl << endl;
                cout << "Please enter Y for Yes and N for No:" << endl << endl;
                getline(cin >> ws, sQuestion);
            }
            else
            {
                cout << sSearchI << " " << "was not found." << endl << endl;
                cout << "Please enter Y for Yes and N for No:" << endl << endl;
                getline(cin >> ws, sQuestion);
            }
        }

    } while (sQuestion == "Y" || sQuestion == "y");


    cout << "Thank you for using this program!" << endl;

    return 0;

}
#包括
#包括
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
int main()
{
向量vName,vID,vClass;
字符串sName、minValue、sID、sClass、sSearch、SQUSTION、ssSearch、SSEARCN、SSEARCI;
智力,是学生;
//显示初始向量大小
iSize=vName.size();

cout好的,在进一步研究代码之后,我想我已经找到了答案

因此,看看排序的代码部分,它的工作方式实际上非常简单。
startScan
是一个int递增,直到它等于for循环中向量的大小。在这种情况下,它是
vName
。minIndex将保存最小的索引号,在初始化时,索引号将相等到
starScan
。最后
minValue
是一个字符串(在本例中,因为我们有一个字符串向量),它充当vName[scanStart]处元素的临时容器。在第二个for循环中,索引将通过向量递增,并将测试元素vName[index]小于我前面提到的temproray容器。如果小于,则新的temp将为vName[index]完成后,它将退出内部循环并更新并移动到vName中的下一个最小值。了解这一点,很容易对其他两个向量进行排序。只需为这些向量创建容器,就像我在这里看到的
minValueA
minValueB
。它们将simp随着vName的变化,您可能会改变。这样,一切都会按照vName的顺序进行。我希望这对某些人有所帮助

int startScan, minIndex;
string minValue, minValueA, minValueB;


    for (startScan = 0; startScan < vName.size() - 1; startScan++)
    {
        minIndex = startScan;
        minValue = vName[startScan];
        minValueA = vID[startScan];
        minValueB = vClass[startScan];

        for (int index = startScan + 1; index < vName.size(); index++)
        {
            if (vName[index] < minValue)
            {
                minValue = vName[index];
                minValueA = vID[index];
                minValueB = vClass[index];
                minIndex = index;

            }
        }
        vName[minIndex] = vName[startScan];
        vName[startScan] = minValue; //values for vName are being added to the other ones.
        vID[minIndex] = vID[startScan];
        vID[startScan] = minValueA;
        vClass[minIndex] = vClass[startScan];
        vClass[startScan] = minValueB;

    }

    //******************


    sort(vName.begin(), vName.end());

    for (int y = 0; y < vName.size(); y++)
    {
        cout << vName[y] << "\t \t \t " << vID[y] << "\t \t \t " << vClass[y] << endl;
    }
intstartscan,minIndex;
字符串minValue、minValueA、minValueB;
对于(startScan=0;startScan对一个“完整的程序”进行了很好的尝试,但它不是一个完整的程序。你能描述什么不起作用吗?它没有正确地分类、崩溃或其他什么东西?它甚至是编译的吗?因为它甚至看起来不像一个有效的C++程序,没有主程序()。很抱歉,我只是想发布相关部分,基本上不是对vName向量进行排序,而是按照填充向量的方式将它们返回。是什么让您认为它排序不正确?我建议您仔细检查代码中的操作顺序。(即,打印,相对于排序)我想我应该问的是,为什么它不按照第一个向量vName的排序方式对所有3个向量进行排序。当我为“(int y=0;y