寻找c+中可枚举的适当容器/函数+; 我试图把一些代码从C++转换成C++,但是缺少字典表/枚举等,使得我很难得到C++中需要的结果。任何人都可以帮助C++中使用的容器类型/方法来获得所需的结果吗?

寻找c+中可枚举的适当容器/函数+; 我试图把一些代码从C++转换成C++,但是缺少字典表/枚举等,使得我很难得到C++中需要的结果。任何人都可以帮助C++中使用的容器类型/方法来获得所需的结果吗?,c++,dictionary,lambda,enumerable,C++,Dictionary,Lambda,Enumerable,提前谢谢 按c1查找所有c1及其计数组,其中c2>0,c3c1;1=>c2;2=>c3(与std::make_tuple中的顺序相同) 如果(std::get(*i)>0&&std::get(*i)(这是相同的,只是不同的符号) std::cout首先这样做应该可以,唯一使用的内存是c1和该c1的有效c2/c3计数: #include <iostream> #include <map> using namespace std; int main() { int

提前谢谢

按c1查找所有c1及其计数组,其中c2>0,c3<4按c1排序

table(c1,c2,c3)  ( number of rows expected is not finite - so - can't use Array as a structure for this )
5 1 2
4 2 3  --> edited this line to make it into the list
4 4 3
4 0 1  --> ignore this row as c2=0
3 1 3  
2 1 5  --> ignore this row as c3 > 4
.....


您至少需要:

  • 用于保存每个c1/c2/c3元组的
    struct
    (如果使用C++11,则为A)
  • 一个(类似数组的容器,但具有动态大小)来保存所有元组
  • 一个(排序关联容器),用作计算输出的字典
我相信这足以让您开始,如果您在实际编写代码时遇到特定问题,请毫不犹豫地提出新问题


根据您的评论进行编辑:

你不会错过太多,elvena的解决方案几乎就是你所需要的,只是它缺少存储对象的向量容器。这很简单:

#include <iostream>
#include <map>
#include <vector>
#include <tuple>

int main()
{
    std::vector<std::tuple<int, int, int>> values;
    while (you_have_more_data) {
        int c1, c2, c3;
        // somehow read c1, c2, c3 from cin/file/whatever
        values.push_back(std::make_tuple(c1, c2, c3));
    }

    std::map<int, int> dict;
    // iterate over the vector
    for (auto i = values.begin(); i != values.end(); ++i) {
        // *i (dereferencing the iterator) yields a std::tuple<int, int, int>
        // use std::get to access the individual values in the tuple
        // 0 => c1; 1 => c2; 2 => c3 (same order as in std::make_tuple)
        if (std::get<1>(*i) > 0 && std::get<2>(*i) < 4)
            dict[std::get<0>(*i)] += 1; // see std::map::operator[]
    }

    // iterate over the map and print its items
    for (auto i = dict.begin(); i != dict.end(); ++i)
        // *i (dereferencing the iterator) yields a std::pair<int, int>
        // but writing (*i).first is cumbersome
        // let's write i->first instead (this is the same, just a different notation)
        std::cout << i->first << " " << i->second << std::endl;

    return 0;
}
#包括
#包括
#包括
#包括
int main()
{
std::向量值;
而(你有更多的数据){
int c1、c2、c3;
//以某种方式从cin/file/which读取c1、c2、c3
push_back(std::make_tuple(c1、c2、c3));
}
std::map dict;
//在向量上迭代
对于(自动i=values.begin();i!=values.end();+i){
//*i(取消对迭代器的引用)生成std::tuple
//使用std::get访问元组中的各个值
//0=>c1;1=>c2;2=>c3(与std::make_tuple中的顺序相同)
如果(std::get(*i)>0&&std::get(*i)<4)
dict[std::get(*i)]+=1;//参见std::map::operator[]
}
//迭代地图并打印其项目
for(自动i=dict.begin();i!=dict.end();++i)
//*i(取消对迭代器的引用)生成一个std::pair
//但是写(*i)。首先很麻烦
//让我们先写i->(这是相同的,只是不同的符号)

std::cout首先这样做应该可以,唯一使用的内存是c1和该c1的有效c2/c3计数:

#include <iostream>
#include <map>

using namespace std;

int main()
{
    int a,b,c = 0;
    map<int, int> n;
    int i;

    for( i = 0 ; i < 6 ; i ++ )
    {
        cout << "Enter three numbers separated by space" << endl;
        cin >> a >> b >> c;
        if( b > 0 &&  c < 4 )
            n[a] += 1;
    }

    for( auto iter = n.begin(); iter != n.end() ; ++iter )
        cout << iter->first << " " << iter->second << endl;

    return 1;
}

请注意,您的示例不适用于c1=4,因为4.2.4在c3规则上失败。

谢谢elvena。但是,这个解决方案看起来并不可行,因为所有这些行都必须通过某种方式存储在一个容器中。谢谢。谢谢syam。我正在使用c++11,实际上开始使用一些类似的路径,但没有取得多大进展。在c#中,我可以制作一个过滤器(视图)然后应用筛选器/组/排序选项以获得所需的输出。我将再次尝试此操作,看看是否可以使用此操作。。Thanks@ejuser:我编辑了我的答案,加入了一个
向量/元组
示例。非常感谢Syam。非常感谢您提供了一个可用的示例,我可以进一步扩展。非常感谢.if(std::get(*i)>0&&std::get(*i)<4)-->这是对我非常有用的语法之一,因为我无法在这口井上更早地得到我的手。
#include <iostream>
#include <map>

using namespace std;

int main()
{
    int a,b,c = 0;
    map<int, int> n;
    int i;

    for( i = 0 ; i < 6 ; i ++ )
    {
        cout << "Enter three numbers separated by space" << endl;
        cin >> a >> b >> c;
        if( b > 0 &&  c < 4 )
            n[a] += 1;
    }

    for( auto iter = n.begin(); iter != n.end() ; ++iter )
        cout << iter->first << " " << iter->second << endl;

    return 1;
}
3 1
4 1
5 1