C++向量“对象”>返回类型函数的实现

C++向量“对象”>返回类型函数的实现,c++,C++,我有下面的程序,如果我在程序的主要功能中实现算法,它会工作得很好。如果我对我的算法做了另一个函数,我就不能使用它,我也不知道问题出在哪里。我应该用一些指针来解决这个问题吗?有人能帮我吗?提前谢谢 #include <algorithm> #include <iostream> #include <vector> using namespace std; class Map { public: float x; float y; fl

我有下面的程序,如果我在程序的主要功能中实现算法,它会工作得很好。如果我对我的算法做了另一个函数,我就不能使用它,我也不知道问题出在哪里。我应该用一些指针来解决这个问题吗?有人能帮我吗?提前谢谢

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;


class Map
{
public:
    float x;
    float y;
    float theta;
};
bool operator<(const Map& x, const Map& y)
{
    return x.theta < y.theta;
}
当我在主功能中实现时,我的排序和过滤算法运行良好:

int main()
{
Map mapArray[10] = {{1, 5, 1}, {9, 2, -0.8}, {3, 0, -6.5}, {5, 7, -4.3}, {1, -4, -0.99}, {6, 4, -0.66}, {10, 8, 6}, {0, 9, 1.1}, {6, 2, 4}, {3, 5, -0.8}};


    vector<Map> resultMap;
    int mapSize = (sizeof mapArray / sizeof mapArray[0]);
    float piHalf = 1.57;
    // Sort the map
    std::sort(mapArray + 0, mapArray + mapSize);
    // Filter the result
    for( int a = 0; a < mapSize; a = a + 1 )
        {
            if(mapArray[a].theta >-piHalf && mapArray[a].theta<piHalf) {
            resultMap.push_back(mapArray[a]);
            }
        }

    // Print each theta-data for test the program
    cout << "Data:" << resultMap.size()<<endl;
    for( unsigned int i = 0; i < resultMap.size(); i = i + 1 )
        {
            cout << "Data:" << resultMap[i].theta<<endl;
        }
   return 0;
}
但是如果我对我的排序和过滤算法做了一个函数,整个过程就不起作用了,结果得到了一个0大小的向量。我的功能实现:

vector<Map> sortAndFilterMap(Map mapArray[]) {

    // Init variables
    vector<Map> resultMap;
    int mapSize = (sizeof mapArray / sizeof mapArray[0]);
    float piHalf = 1.57;
    // Sort the map
    std::sort(mapArray + 0, mapArray + mapSize);
    // Filter the result
    for( int a = 0; a < mapSize; a = a + 1 )
        {
            if(mapArray[a].theta >-piHalf && mapArray[a].theta<piHalf) {
            resultMap.push_back(mapArray[a]);
            }
        }
    return resultMap;
    }
我主要如何使用它:

int main() {
Map mapArray[10] = {{1, 5, 1}, {9, 2, -0.8}, {3, 0, -6.5}, {5, 7, -4.3}, {1, -4, -0.99}, {6, 4, -0.66}, {10, 8, 6}, {0, 9, 1.1}, {6, 2, 4}, {3, 5, -0.8}};

    vector<Map> resultMap;
    resultMap = sortAndFilterMap(mapArray);



    // Print each theta-data for test the program
    cout << "Data:" << resultMap.size()<<endl;
    for( unsigned int i = 0; i < resultMap.size(); i = i + 1 )
        {
            cout << "Data:" << resultMap[i].theta<<endl;
        }
   return 0;
}
编辑:很抱歉,我没有很好地解释我的错误。错误如下:如您所见,在主函数的末尾,我打印了过滤和排序后的地图。在第一个解决方案中,当我在主循环中实现时,我得到了预期值。当我做一个函数时,θ值也应该打印出来,但我什么也没打印出来。在for循环之前,我无法计算结果映射的大小,但得到的是0,而不是6

int mapSize = (sizeof mapArray / sizeof mapArray[0]);
这条线只在极少数情况下有效。说你不应该这样做更容易。将数组的长度作为额外参数传递,这样您就安全了:

std::vector<Map> sortAndFilterMap(Map mapArray[], int mapSize)
或者,您可以到处使用std::vector并一起删除原始数组。

int-mapSize=sizeof-mapArray/sizeof-mapArray[0]的结果;在这两种情况下是不同的:

当它在主要:结果是10,这是你所期望的。 当它位于vector SortAndFilterMap mapArray[]中时:数组衰减为指向贴图对象的指针,函数不再知道mapArray的大小;sizeof mapArray根据您是在32位还是64位系统上,返回此类指针的大小4或8字节。因此,结果是4或8/sizeof float*3=0。for循环将在第一次迭代时立即中断,函数返回一个空向量。
我不想鼓励使用原始C数组,除非在非常特殊的情况下,否则基本上不应该使用原始C数组。但是,如果真的有必要,您可以使用以下方法:

template<size_t mapSize>
vector<Map> sortAndFilterMap(Map (&mapArray)[mapSize]) {
对数组&mapArray的引用可以使用模板推断来推断数组的大小。在此映射中,大小将具有正确的大小


无论如何,默认情况下只需使用向量而不是C数组。

它不起作用,没有说明错误是什么。告诉我们错误。如果我是你,我没有使用命名空间std;编写;。我很抱歉。错误如下:如您所见,在主函数的末尾,我打印了过滤和排序后的地图。在第一个解决方案中,当我在主循环中实现时,我得到了预期值。当我做一个函数时,θ值也应该打印出来,但我什么也没打印出来。在for循环之前,我无法确定结果映射的大小,但我得到的是0,而不是6。我建议您不要命名类映射。这很令人困惑,因为在std库中已经使用MAPI来命名关联数组容器。@Emadpres它的名称是“我将数组的长度添加为额外参数”。尽管在主循环中,我使用int-mapSize=sizeof-mapArray/sizeof-mapArray[0]定义数组的长度;有没有其他方法来定义一个简单数组的长度?好吧,您已经将大小设置为固定的,所以您可以说const int MapSize=10;一旦您使用另一个函数,就无法实际获取数组的大小。您需要额外的值。在C++中这样做的习惯用法是:有STD::vector SoTrand Fieldmap map const *,map const *结束,并用SoTrand FieldMalmap STD调用它::开始MaPARStudio,STD::至少,在真正需要C样式数组的情况下。在他的特殊情况下,我会使用std::vector做所有事情。@mirind4来获取C样式数组的长度:std::end array-std::begin array..更好的是让sortAndFilterMap取两个迭代器,并传递给它std::begin mapArray和std::end mapArray。很好的解释,现在我明白了为什么会出现问题,谢谢!: