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_Vector_Alphabetical - Fatal编程技术网

C++ 按索引值按字母顺序排序向量

C++ 按索引值按字母顺序排序向量,c++,sorting,vector,alphabetical,C++,Sorting,Vector,Alphabetical,我有一个向量,我想按字母顺序排序。我已经成功地按照一个索引值按字母顺序对它进行排序,但是当我这样做时,它只会改变索引的顺序,而不会改变整个向量。我怎样才能使它将顺序更改应用于整个向量? 这是我当前运行的代码: std::sort (myvector[2].begin(), myvector[2].end(), compare); bool icompare_char(char c1, char c2) { return std::toupper(c1) < std::toupper(

我有一个向量,我想按字母顺序排序。我已经成功地按照一个索引值按字母顺序对它进行排序,但是当我这样做时,它只会改变索引的顺序,而不会改变整个向量。我怎样才能使它将顺序更改应用于整个向量? 这是我当前运行的代码:

std::sort (myvector[2].begin(), myvector[2].end(), compare);

bool icompare_char(char c1, char c2)
{
  return std::toupper(c1) < std::toupper(c2);
}

bool compare(std::string const& s1, std::string const& s2)
{
  if (s1.length() > s2.length())
    return true;
  if (s1.length() < s2.length())
    return false;
  return std::lexicographical_compare(s1.begin(), s1.end(),
                                      s2.begin(), s2.end(),
                                      icompare_char);
}
例如,如果我有一个向量:

myvector[0][0] = 'One' AND myvector[2][0]='b'
myvector[0][1] = 'Two' AND myvector[2][1]='a'
myvector[0][2] = 'Three' AND myvector[2][2]='c'

| One | Two | Three |
|  1  |  2  |   3   |
|  b  |  a  |   c   |
我把它分类,我得到:

myvector[0][0] = 'One' AND myvector[2][0]='a'
myvector[0][1] = 'Two' AND myvector[2][1]='b'
myvector[0][2] = 'Three' AND myvector[2][2]='c'

| One | Two | Three |
|  1  |  2  |   3   |
|  a  |  b  |   c   |
而不是我想要的:

myvector[0][0] = 'Two' AND myvector[2][0]='a'
myvector[0][1] = 'One' AND myvector[2][1]='b'
myvector[0][2] = 'Three' AND myvector[2][2]='c'

| Two | One | Three |
|  2  |  1  |   3   |
|  a  |  b  |   c   |
我四处寻找一个好的方法,但找不到任何有效的方法。。。我的想法是:

std::sort (myvector.begin(), myvector.end(), compare);

然后在我的比较函数中处理第三个索引的排序,以便编辑整个向量。。。但是,当我传递数据时,我要么只是更改了函数中的顺序,仍然没有更改顶层,要么出现了错误。任何建议或帮助都将不胜感激。提前谢谢。

理想情况下,将3个数据字段合并到一个
结构中
,这样您就可以只使用1个向量,并对其进行简单排序

struct DataElement{
    std::string str;
    char theChar;
    int num;
    bool operator<(const DataElement& other)const{return theChar<other.theChar;}
};

std::vector<DataElement> myvector;

std::sort (myvector.begin(), myvector.end());
struct数据元素{
std::字符串str;
炭黑;
int-num;

bool操作符理想情况下,将3个数据字段合并为一个
结构
,这样您就可以只拥有1个向量,并对其进行简单排序

struct DataElement{
    std::string str;
    char theChar;
    int num;
    bool operator<(const DataElement& other)const{return theChar<other.theChar;}
};

std::vector<DataElement> myvector;

std::sort (myvector.begin(), myvector.end());
struct数据元素{
std::字符串str;
炭黑;
int-num;

bool operation您在您的what-I-have/what-I-want描述中遗漏了
myvector[1][…]
。有什么原因吗?如果您想按第二个索引排序,那么
[2]
应该出现在
compare
中。这将对行进行排序,而不是像您所示的列,因此您应该转置数据。@whozCraig:我刚刚省略了1索引,以减少你们的阅读量。我只是使用带索引的向量来描述基本结构。您省略了
myvector[1][…]
在您的“我拥有什么/我想要什么”描述中。有什么原因吗?如果您想按第二个索引排序,那么
[2]
应该出现在
compare
中。这将对行进行排序,而不是像您所示的列,因此您应该转置数据。@whozCraig:我只是省略了1索引,以减少你们的阅读量。我只是使用带索引的向量来描述基本结构。我以前没有使用过structs,因为我很聪明新的C++。这个想法是有意义的,但是在读了一点之后,我会引用MyValth[0 ]之类的东西。STR =“一”和MyValth[0 ]。Char =“B”和MyValth[0 ]。.num=1?那么我如何才能按myvector.theChar为每个索引编辑我的排序?@DevonBernard请查看我的编辑,看看这是否是您想要的。它将按CHAR的升序对整个列表进行排序。如果您不提供比较函数,默认值是使用
@Kerthik T:好的,我刚刚实现了您的代码,我成功了s完全能够将所有值插入向量结构。但是,当我尝试对其进行排序时,会出现一个非常长的错误。@DevonBernard这没有帮助:你能复制粘贴到类似的东西上吗?好的,我也发布了这个错误,谢谢你的帮助。我以前没有使用过结构,因为我对C++是很新的。这个想法是有意义的,但是在读了一点之后我会引用MyVoice(0)之类的东西。STR =“一”和MyValth[0 ]。和myvector[0].num=1?那么我如何才能按myvector.theChar为每个索引编辑我的排序?@DevonBernard请查看我的编辑,看看这是否是您想要的。它将按CHAR的升序对整个列表进行排序。如果您不提供比较函数,默认值是使用
@Kerthik T:好的,我刚刚实现了您的代码,我成功了S完全能够将所有值插入向量结构。但是,当我尝试对其进行排序时,会出现一个非常长的错误。@DevonBernard,这没有帮助:(你能将其复制粘贴到类似的内容中并链接到此处吗?好的,我在上发布了错误,也非常感谢你的帮助!