C++ 含向量结构的排序

C++ 含向量结构的排序,c++,C++,我试图对结构的向量进行排序。以下是我的代码。我无法正确排序…有人能帮我吗?我需要根据mmNo进行排序。对不起…我遗漏了部分代码 typedef struct MMInfo { std :: string strMmNo; std :: string strMmName; std :: string strMmsPlace; std :: string strMmAdd; std :: string strMmPh; MMInfo(const std::string&am

我试图对结构的向量进行排序。以下是我的代码。我无法正确排序…有人能帮我吗?我需要根据mmNo进行排序。对不起…我遗漏了部分代码

typedef struct MMInfo
{
  std :: string strMmNo;
  std :: string strMmName;
  std :: string strMmsPlace;
  std :: string strMmAdd;
  std :: string strMmPh;

  MMInfo(const std::string& mmNo,
         const std::string& mmName,
         const std::string& mmPlace,
         const std::string& mmAdd,
         const std::string& mmPh) : stringValue(mmNo,),stringValue(mmName),
                                    stringValue(mmPlace),stringValue(mmAdd),
                                    stringValue(mmPh) {}

  bool operator < (const MMInfo& str) const
  {
    return (mmNo < str.mmNo);
  }

} MMInfo;

std::vector < MMInfo > mmlist;
MMInfo mmInfo = {"", "", "", "", ""};

mmInfo.strMmNo = "3452132";   //actually , i have used a loop to get it from the user
mmInfo.strMmName="Peter";
mmInfo.strMmPlace="TCR";
mmInfo.strMmAdd="Street 453";
mmInfo.strMmPh="8587556587";

mmlist.push_back(mmInfo);

sort(mmlist.begin(),mmlist.end());
for (int i=0; i<mmlist.size(); i++)
{
  cout << " first row :" << mmlist[i].strMmNo << " " << mmlist[i].strMmName 
       <<" " <<mmlist[i].strMmsPlace  << " " << mmlist[i].strMmsAdd ;
}

那还可以编译吗?不

您正在尝试初始化不存在的值。也许初始化strMm*值而不是stringValue? 您正在初始化一个根本不存在的值!多次使用相应的mm*值初始化strMm*成员 比较函数比较不存在的值。比较strMmNo而且,您的列表中甚至没有可排序的值。

您的代码没有问题。我的意思是,用法是正确的,但结构定义似乎不正确。它至少在Visual C++ 9中是不可编译的,请制作一个正确的初始化列表。下面的代码对我来说很好

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

using namespace std;

typedef struct NodeInfo
{
    int x;

    NodeInfo( int xi ){ x = xi;  }
    bool operator < (const NodeInfo& str) const
    {
        return (x < str.x);
    }
}MMInfo;

int _tmain(int argc, _TCHAR* argv[])
{
    std::vector < MMInfo > mmlist;

    mmlist.push_back( 1 );
    mmlist.push_back( 31 );
    mmlist.push_back( 21 );
    mmlist.push_back( 11 );
    mmlist.push_back( 41 );
    sort(mmlist.begin(),mmlist.end());
    for (unsigned int i=0; i<mmlist.size(); i++)
    {
        cout<< " x row : \n" << mmlist[i].x ;
    }
    return 0;
}

为什么?发生了什么不正常的事情?这段代码甚至没有编译。问题是它为什么不编译?mmlist是空的,请继续,这里没有排序。或者问题是它为什么按照词汇顺序而不是数字顺序排序?@vidhya:你发布了假的不可编译代码,没有解释它的错误。你希望有人如何回答你的问题?