识别结构C ++中的唯一元素 [英] Identify unique elements in a structure C++

查看:65
本文介绍了识别结构C ++中的唯一元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个结构,其中包含有关几何图形的信息.我的问题是我不知道显示唯一的图形的体积(这意味着另一个图形没有相同的体积).如果有人知道,请帮助我.

I have a structure that contains information about a geometrical figure. My problem is that I don't know to display the volume of figures that are unique (it means that another figure does not have the same volume). If someone knows, please help me.

struct Sfere
    {
      char codsf[5];
      char colour[15];
      char material[15];
      float r,area,volume;
    } sf[100]

推荐答案

sf [i] .volume 访问结构数组第i个元素的体积字段

sf[i].volume access the volume field of the i-th element of your array of structures

要使算法查找体积的唯一值,它取决于您的数据集.

For the algorithm to find unique values of volumes, it depends on your dataset.

最通用的方法是使用'volume'作为键对数组进行排序.以这种方式对数组进行排序时,只需忽略相同值的连续条目,就很容易计算唯一条目.

The most generic approach would be to sort your array using 'volume' as key. When your array is sorted this way, it's easy to count the unique entries by just ignoring consecutive entries of the same value.

示例:

"1 6 7 2 7 9 7"变为"1 2 6 7 7 7 9 9",则您删除了相同值的连续条目或忽略了"1 2 6 7 9".并且您具有唯一卷条目的列表.

'1 6 7 2 7 9 7' becomes '1 2 6 7 7 7 9' you strip away consecutive entries of same value or ignore them '1 2 6 7 9'. and you have a list of unique volume entries.

更新:

@Max Langhof建议使用std :: unique更干净.您只需要使用std :: vector来具有迭代器和lambda即可指定要处理的字段.

@Max Langhof suggestion to use std::unique is cleaner. You only need to use std::vector to have iterators and lambda to specify the field you are working on.


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

int main()
{
    typedef struct _Sfere
    {
        char codsf[5];
        char colour[15];
        char material[15];
        float r,area,volume;
    } Sfere;

    //Encapsulate structure in a standard vector to have iterators
    std::vector<Sfere> my_sfere_vector(10);
    //Insert unique elements
    my_sfere_vector[1].volume = 1.0;
    my_sfere_vector[7].volume = 3.0;
    //Show content
    for (std::vector<Sfere>::iterator my_element = my_sfere_vector.begin();my_element != my_sfere_vector.end();my_element++)
    {
        std::cout << " " << my_element->volume;
    }
    std::cout << "\n";
    //Sort the vector
    std::sort
    (
        my_sfere_vector.begin(),
        my_sfere_vector.end(),
        [](const Sfere &a, const Sfere &b)
        {
            return a.volume < b.volume;
        }
    );
    //Unique elements
    std::vector<Sfere>::iterator last = std::unique
    (
        my_sfere_vector.begin(),
        my_sfere_vector.end(),
        [](const Sfere &a, const Sfere &b)
        {
            return a.volume == b.volume;
        }
    );
    //Prune out excess elements
    my_sfere_vector.erase(last, my_sfere_vector.end());
    //Show content
    for (std::vector<Sfere>::iterator my_element = my_sfere_vector.begin();my_element != my_sfere_vector.end();my_element++)
    {
        std::cout << " " << my_element->volume;
    }
    std::cout << "\n";
}

这是结果


     0 1 0 0 0 0 0 3 0 0
     0 1 3

    Process returned 0 (0x0)   execution time : 0.015 s
    Press any key to continue.

这篇关于识别结构C ++中的唯一元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆