【问题标题】:Platform::Collections::Vector sortingPlatform::Collections::Vector 排序
【发布时间】:2013-08-16 19:39:24
【问题描述】:

在 Metro Style 应用程序中,有时我们使用 Platform::Collections::Vector 来保存 ListView 中使用的元素。

如何对Platform::Collections::Vector 进行排序?

我知道 std 中有很多可以排序的结构,但我想知道除了编写自己的排序函数之外,是否还有其他方法用于 Platform::Collections::Vector

【问题讨论】:

    标签: windows-8 microsoft-metro c++-cx


    【解决方案1】:

    实际上像下面这样的东西也应该起作用:

    auto vec = ref new Platform::Collections::Vector<T^>();
    std::sort(begin(vec), end(vec));
    

    【讨论】:

    • 你可以使用stdCX 类型吗?
    • 你可以在 CX 容器上使用 std 算法
    • 编译器不允许在这个 vec 中使用 begin 和 end
    • 我设计并实现了,我可以确认拉曼是正确的。包含 后,在 WFC::IVector^ 或 PC::Vector^ 上调用不合格的非成员 begin()/end() 将返回 PC::VectorIterator,一个普通的 C++ 类,它是一个随机访问 STL 迭代器。它可以与 std::sort() 等 STL 算法一起使用。请注意,std::sort() 会希望将您的元素与 op
    • @ppaulojr 我刚刚被这个 Visual Studio 2015 RC 绊倒了。我的问题是我使用的是 std::begin 但这些集合的非成员开始函数存在于 WFC 命名空间中。像上面的答案一样删除 std:: 解决了我的问题,因为我有 WFC 命名空间的 using 命名空间语句。也许这就是你遇到的问题。
    【解决方案2】:

    我没有找到任何合适的答案,所以我使用了这个解决方法。

    这是一个简单的quicksort Platform::Collections::Vector

    void swap (Platform::Collections::Vector<T^>^ vec, int pos1, int pos2)
    {
       T^ tmp = vec->GetAt(pos1);
       vec->SetAt(pos1, vec->GetAt(pos2));
       vec->SetAt(pos2,tmp);
    }
    
    int compare (T^ c1, T^ c2)
    {
       int c = wcscmp (c1->Title->Data(),c2->Title->Data());
       return -c;
    }
    
    int PartitionVec (int left, int right, 
                                Platform::Collections::Vector<T^>^ vec)
    {
       int i,j;
       i = left;
       for (int j = left + 1; j <= right; ++j)
       {
           if (compare (vec->GetAt(j),vec->GetAt(left)) > 0)
           {
                   ++i;
               swap (vec,i,j);
           }
        }
        swap (vec,left,i);
        return i;
    }
    
    void QuickSortVec (Platform::Collections::Vector<T^>^ vec,
                                               int start, int end)
    {
       if (end > start)
       {
           int pivot_point;
           pivot_point = PartitionVec (start, end, vec);
           QuickSortVec (vec,start,pivot_point - 1);
           QuickSortVec (vec, pivot_point + 1, end);
        } 
    }
    

    【讨论】:

      猜你喜欢
      • 2017-02-26
      • 2012-08-20
      • 1970-01-01
      • 2017-09-21
      • 1970-01-01
      • 2011-08-07
      • 1970-01-01
      • 2016-12-08
      • 2020-09-07
      相关资源
      最近更新 更多