C++ 使用字符串向量进行插入排序

C++ 使用字符串向量进行插入排序,c++,vector,insertion-sort,C++,Vector,Insertion Sort,我正在尝试使用插入排序对字符串向量进行排序 这是我的代码: void insertionsort(std::vector<std::string> &strings) { typedef std::vector<std::string>::size_type size_type; for(size_type i = 0;i < strings.size(); i++) { std::string const tmp

我正在尝试使用插入排序对字符串向量进行排序

这是我的代码:

void insertionsort(std::vector<std::string> &strings) 
{
    typedef std::vector<std::string>::size_type size_type;
    for(size_type i = 0;i < strings.size(); i++) 
    {
        std::string const tmp = strings[i];
        size_type j = i - 1;
        while(j >= 0 && tmp < strings[j]) //this is the problem
        {
            strings[j + 1]= strings[j];
            j--;

        }
        strings[j + 1]=tmp;
    }
}
然后它给了我:

2 line1
1 line5
3 line2
4 line3
5 line4

vector::size\u type
是无符号的,因此
j>=0
不能为false。您应该使用
vector::difference\u type
vector::size\u type
是无符号的,因此
j>=0
不能为false。您应该使用
vector::difference\u type
类模板
std::vector
的类型别名
size\u type
始终是非负整数类型。那么,美国的经济衰退呢

j >= 0
这永远是真的

您需要的是在函数实现中做一些小的更改。显然,只包含一个元素的向量总是被排序的。因此,您应该以索引等于1开始外部循环

给你

#include <iostream>
#include <vector>
#include <string>

void insertionSort( std::vector<std::string> &strings ) 
{
    typedef std::vector<std::string>::size_type size_type;

    for ( size_type i = 1; i < strings.size(); ++i ) 
    {
        std::string tmp = strings[i];

        size_type j = i;

        for ( ; j !=  0 && tmp < strings[j-1]; --j )
        {
            strings[j] = strings[j-1];
        }

        if ( j != i ) strings[j] = tmp;
    }
}

int main() 
{
    std::vector<std::string> v = { "E", "D", "C", "B", "A" };

    for ( const auto &s : v ) std::cout << s << ' ';
    std::cout << std::endl;

    insertionSort( v );

    for ( const auto &s : v ) std::cout << s << ' ';
    std::cout << std::endl;
}   
请注意这一补充声明

if ( j != i ) strings[j] = tmp;
如果一个元素在向量中已经占据了所需的位置,那么将它分配给它自己就没有意义了。这使该功能更加高效


将类型
difference\u type
与类型
size\u type
混合是一个坏主意,后者是成员函数
size()的返回类型
类模板
std::vector
的类型别名
size\u type
始终是非负整数类型。那么,美国的经济衰退呢

j >= 0
这永远是真的

您需要的是在函数实现中做一些小的更改。显然,只包含一个元素的向量总是被排序的。因此,您应该以索引等于1开始外部循环

给你

#include <iostream>
#include <vector>
#include <string>

void insertionSort( std::vector<std::string> &strings ) 
{
    typedef std::vector<std::string>::size_type size_type;

    for ( size_type i = 1; i < strings.size(); ++i ) 
    {
        std::string tmp = strings[i];

        size_type j = i;

        for ( ; j !=  0 && tmp < strings[j-1]; --j )
        {
            strings[j] = strings[j-1];
        }

        if ( j != i ) strings[j] = tmp;
    }
}

int main() 
{
    std::vector<std::string> v = { "E", "D", "C", "B", "A" };

    for ( const auto &s : v ) std::cout << s << ' ';
    std::cout << std::endl;

    insertionSort( v );

    for ( const auto &s : v ) std::cout << s << ' ';
    std::cout << std::endl;
}   
请注意这一补充声明

if ( j != i ) strings[j] = tmp;
如果一个元素在向量中已经占据了所需的位置,那么将它分配给它自己就没有意义了。这使该功能更加高效

将类型
difference\u type
与类型
size\u type
混合使用是一个坏主意,该类型是成员函数
size()

的返回类型。(这不是无可争议的,但标准委员会的几位杰出成员都支持我的观点)。请使用签名字体。(这不是无可争议的,但标准委员会的几位杰出成员都支持我的观点)。