简体   繁体   English

从向量中提取不包含0的字符串 <pair<string, int> &gt;

[英]Extract strings that don't contain 0 from vector<pair<string, int>>

I'm trying to find out strings that don't have zero. 我试图找出不为零的字符串。

The incoming data: (strings are in order but values or not) 传入数据:(字符串按顺序排列,但不包含值)

std::vector<std::pair<std::string, int>> data =
    {
        {"A", 3},
        {"A", 0},
        {"A", 1},
        {"B", 2},
        {"B", 0},
        {"C", 2},
        {"D", 0},
        {"D", 1},
        {"E", 3},
        {"E", 4}
    };

The result I want to get: (strings that don't contain zero) 我想得到的结果:(不包含零的字符串)

C, E C,E

Here's my so far code that doesn't work: 这是到目前为止我无法使用的代码:

#include <iostream>
#include <string>
#include <vector>
#include <utility>

int main()
{
    std::vector<std::pair<std::string, int>> data =
    {
        {"A", 3},
        {"A", 0},
        {"A", 1},
        {"B", 2},
        {"B", 0},
        {"C", 2},
        {"D", 0},
        {"D", 1},
        {"E", 3},
        {"E", 4}
    };
    std::string previousStr = "";
    bool hasZero = false;
    std::vector<std::string> nonZeroStrs;
    for (size_t i = 0; i < data.size(); ++i)
    {
        std::string currentStr = data[i].first;
        if (currentStr != previousStr)
        {
            if (previousStr != "")
            {
                if (!hasZero)
                    nonZeroStrs.push_back(previousStr);
            }
        }
        if (data[i].second == 0)
        {
            hasZero = true;
        }
        previousStr = currentStr;
    }
    for (size_t i = 0; i < nonZeroStrs.size(); ++i)
    {
        std::cout << nonZeroStrs[i] << '\n';
    }
    return 0;
}

You can use a map to remember if a certain key should not be included based on its second pair member. 您可以使用地图来记住是否应基于其第二对成员不包括某个特定钥匙。

std::unordered_map<std::string, bool> invalid;
for (auto const& p : data) {
    if (p.second == 0) {
        invalid[p.first] = true;
    }
}
for (auto const& p : data) {
    if (!invalid[p.first]) {
        std::cout << p.first << '\n';
        invalid[p.first] = true;
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从向量对的向量向量插入向量串的向量<int,string> - inserting in a vector of vector string from vector vector of vector pair<int,string> 用字符串和整数对排序矢量 - Sort Vector with string and int pair 如何复制向量中的字符串<pair<string, int> &gt; 到矢量<string> ? </string></pair<string,> - How do I copy the strings in a vector<pair<string, int>> to vector<string>? 在向量上使用std :: sort的Segfault <pair<string, int> &gt; - Segfault from using std::sort on vector<pair<string, int>> 如何对vector &lt;pair &lt;int,pair进行排序 <int , pair<string , pair<int , int > &gt;&gt;&gt;&gt;? - How to sort vector< pair< int , pair<int , pair<string , pair<int , int > > > > >? 如何对矢量进行排序 <pair<string , pair<int , int> &gt;&gt;? - How to sort a vector<pair<string , pair<int , int>>>? 为什么我不能像这样插入对值? v.push_back(矢量<pair<int,string> &gt;({1,"A"}));? </pair<int,string> - Why I can't insert the pair value like this? v.push_back(vector<pair<int,string> >({1,"A"}));? 地图 <string, vector <pair<int, int> &gt;&gt;推回成对? - map<string, vector <pair<int, int> > > pushing back into pair? 如何转换矢量<pair<string, t> &gt; 到矢量<pair<string,string> &gt; </pair<string,string></pair<string,> - How to convert vector<pair<string, T>> to vector<pair<string,string>> 从函数填充的字符串的推导向量中的字符串到int - String to int from deduced vector of strings filled by a function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM