如何将文件中的不同行号存储到一个集合中?

Posted

技术标签:

【中文标题】如何将文件中的不同行号存储到一个集合中?【英文标题】:How to store different line numbers from file into a set? 【发布时间】:2021-03-04 22:32:25 【问题描述】:

我正在尝试从文本文件中获取单词并将其存储为地图的键值,而对于值,包含每个单词出现在文件中的不同行的集合。我不清楚如何从每个单词中获取行号并将其存储在一组中。我到目前为止的代码如下。

#include <set>
#include <map>
#include <algorithm>
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;


int main()

    ifstream myfile;
    string word;
    vector<string> list;
    set<int> lines;
    int linecount = 0;
    myfile.open("kennedy.txt");
    if (myfile.fail()) 
        cout << "No file found";
    
    else
        while (myfile >> word) 
            list.push_back(word);
        
    for (auto element : list)
        cout << element << endl;

    map<string, int> word1 = list.at(0),/*set*/ ;

【问题讨论】:

【参考方案1】:

这是我的解决方案:

int main()

    ifstream myfile;
    string word;
    int linecount = 1;
    set<int> lines; //the default set, which we use only to initialize
    map<string, set<int>> wordmap;

    myfile.open("kennedy.txt");
    if (!myfile)  //if an error happened. Same as myfile.fail(), myfile.bad(), and myfile.eof() combined.
        cout << "No file found";
    
     //no need for the else that was here before
    while (getline(myfile, word))  //for each line
        stringstream wordstream;
        wordstream << word; //put the line into a stringstream
        while (wordstream >> word)  //for every word we get out of wordstream
            if (!wordmap.count(word)) wordmap.insert( word, lines ); //if it is not in our map, add it with the default set
            auto current = wordmap.find(word); //get an iterator to the word
            current->second.insert(linecount); //add the current line to the word's set of ints
        
        ++linecount; //increment the linecount
    

    //Example of how to access the elements:

    for (auto element : wordmap) 
        cout << "Key: " << element.first << " Values: ";
        for (int item : element.second) 
            cout << item << " ";
        
        cout << endl;
    


cmets 几乎解释了我的所作所为。

我使用从https://***.blog/2021/04/13/level-up-creative-coding-with-p5-js-parts-4-and-5/?cb=1&_ga=2.159343934.584816135.1618070092-708873185.1600384169 复制/粘贴的文本对此进行了测试。 .txt 文件如下所示:


Level Up: Creative Coding with p5.js – parts 4 and 5
Part 4 In this session, we will make our p5.js sketches interactive! Together with Morgan, we will work on the Generative Art Creator project using the p5.js web editor! We will make a tool to create, control, and combine generative works of art inspired by early media artists, such as John Whitney and Vera Molnar.…
Avatar for 
Jiwon Shin
Part 4
In this session, we will make our p5.js sketches interactive! Together with Morgan, we will work on the Generative Art Creator project using the p5.js web editor! We will make a tool to create, control, and combine generative works of art inspired by early media artists, such as John Whitney and Vera Molnar. We will map different visualizations to be triggered by each key on the keyboard.


Here are some Stack Overflow questions related to the work we did in this session:

How to trigger key events in p5.js

How to make something happen when a key is held down in p5.js

How do I hold down a key in p5 instead of just pressing it repeatedly?

How do I save a p5.js canvas as a very large PNG?

If you enjoyed this lesson, you can catch up on the rest of the series on YouTube. If you’d like to watch a session live, follow the Codecademy YouTube channel.

Every Tuesday from now until April 27th, we’ll be streaming a new session at 4PM EST. You can set a reminder for the stream for April 6th here.

Finally, if you want even more Creative Coding with p5.js content, you can sign up for the interactive course this series was based on here. This course was developed by Jiwon and has many more quizzes, projects, and helpful articles that we can’t fit into our streams!

Part 5
In this session, we will learn how to display and manipulate images and videos in our p5.js sketches. Together, we will work on the Interactive Video Sculpture project using the p5.js web editor! We will create a digital video sculpture inspired by the artist Nam Jun Paik. Applying what we learned in our last session, we’ll also add mouse interaction to dynamically change the videos displayed in the sculpture.


Here are some Stack Overflow questions related to the work we did in this session:

p5.js Play Video inside canvas

Stretch a video in p5.js?

If you enjoyed this lesson, you can catch up on the rest of the series on YouTube. If you’d like to watch a session live, follow the Codecademy YouTube channel.

Every Tuesday from now until April 27th, we’ll be streaming a new session at 4PM EST. You can set a reminder for the stream for April 13th here.

Finally, if you want even more Creative Coding with p5.js content, you can sign up for the interactive course this series was based on here. This course was developed by Jiwon and has many more quizzes, projects, and helpful articles that we can’t fit into our streams!

Tags: codeacademy, creative coding, level up, p5.js

输出是:

Key: 13th Values: 38
Key: 27th, Values: 22 38
Key: 4 Values: 2 3 6
Key: 4PM Values: 22 38
Key: 5 Values: 2 26
Key: 6th Values: 22
Key: Applying Values: 27
Key: April Values: 22 38
Key: Art Values: 3 7
Key: Avatar Values: 4
Key: Codecademy Values: 20 36
Key: Coding Values: 2 24 40
Key: Creative Values: 2 24 40
Key: Creator Values: 3 7
Key: EST. Values: 22 38
Key: Every Values: 22 38
Key: Finally, Values: 24 40
Key: Generative Values: 3 7
Key: Here Values: 10 30
Key: How Values: 12 14 16 18
Key: I Values: 16 18
Key: If Values: 20 36
Key: In Values: 3 7 27
Key: Interactive Values: 27
Key: Jiwon Values: 5 24 40
Key: John Values: 3 7
Key: Jun Values: 27
Key: Level Values: 2
Key: Molnar. Values: 7
Key: Molnar.… Values: 3
Key: Morgan, Values: 3 7
Key: Nam Values: 27
Key: Overflow Values: 10 30
Key: PNG? Values: 18
Key: Paik. Values: 27
Key: Part Values: 3 6 26
Key: Play Values: 32
Key: Sculpture Values: 27
Key: Shin Values: 5
Key: Stack Values: 10 30
Key: Stretch Values: 34
Key: Tags: Values: 42
Key: This Values: 24 40
Key: Together Values: 3 7
Key: Together, Values: 27
Key: Tuesday Values: 22 38
Key: Up: Values: 2
Key: Vera Values: 3 7
Key: Video Values: 27 32
Key: We Values: 3 7 27
Key: Whitney Values: 3 7
Key: You Values: 22 38
Key: YouTube Values: 20 36
Key: YouTube. Values: 20 36
Key: a Values: 3 7 14 16 18 20 22 27 34 36 38
Key: add Values: 27
Key: also Values: 27
Key: and Values: 2 3 7 24 27 40
Key: are Values: 10 30
Key: art Values: 3 7
Key: articles Values: 24 40
Key: artist Values: 27
Key: artists, Values: 3 7
Key: as Values: 3 7 18
Key: at Values: 22 38
Key: based Values: 24 40
Key: be Values: 7 22 38
Key: by Values: 3 7 24 27 40
Key: can Values: 20 22 24 36 38 40
Key: canvas Values: 18 32
Key: canΓÇÖt Values: 24 40
Key: catch Values: 20 36
Key: change Values: 27
Key: channel. Values: 20 36
Key: codeacademy, Values: 42
Key: coding, Values: 42
Key: combine Values: 3 7
Key: content, Values: 24 40
Key: control, Values: 3 7
Key: course Values: 24 40
Key: create Values: 27
Key: create, Values: 3 7
Key: creative Values: 42
Key: developed Values: 24 40
Key: did Values: 10 30
Key: different Values: 7
Key: digital Values: 27
Key: display Values: 27
Key: displayed Values: 27
Key: do Values: 16 18
Key: down Values: 14 16
Key: dynamically Values: 27
Key: each Values: 7
Key: early Values: 3 7
Key: editor! Values: 3 7 27
Key: enjoyed Values: 20 36
Key: even Values: 24 40
Key: events Values: 12
Key: fit Values: 24 40
Key: follow Values: 20 36
Key: for Values: 4 22 24 38 40
Key: from Values: 22 38
Key: generative Values: 3 7
Key: happen Values: 14
Key: has Values: 24 40
Key: held Values: 14
Key: helpful Values: 24 40
Key: here. Values: 22 24 38 40
Key: hold Values: 16
Key: how Values: 27
Key: if Values: 24 40
Key: images Values: 27
Key: in Values: 10 12 14 16 27 30 34
Key: inside Values: 32
Key: inspired Values: 3 7 27
Key: instead Values: 16
Key: interaction Values: 27
Key: interactive Values: 24 40
Key: interactive! Values: 3 7
Key: into Values: 24 40
Key: is Values: 14
Key: it Values: 16
Key: just Values: 16
Key: key Values: 7 12 14 16
Key: keyboard. Values: 7
Key: large Values: 18
Key: last Values: 27
Key: learn Values: 27
Key: learned Values: 27
Key: lesson, Values: 20 36
Key: level Values: 42
Key: like Values: 20 36
Key: live, Values: 20 36
Key: make Values: 3 7 14
Key: manipulate Values: 27
Key: many Values: 24 40
Key: map Values: 7
Key: media Values: 3 7
Key: more Values: 24 40
Key: mouse Values: 27
Key: new Values: 22 38
Key: now Values: 22 38
Key: of Values: 3 7 16 20 36
Key: on Values: 3 7 20 24 27 36 40
Key: our Values: 3 7 24 27 40
Key: p5 Values: 16
Key: p5.js Values: 2 3 7 12 14 18 24 27 32 40 42
Key: p5.js? Values: 34
Key: parts Values: 2
Key: pressing Values: 16
Key: project Values: 3 7 27
Key: projects, Values: 24 40
Key: questions Values: 10 30
Key: quizzes, Values: 24 40
Key: related Values: 10 30
Key: reminder Values: 22 38
Key: repeatedly? Values: 16
Key: rest Values: 20 36
Key: save Values: 18
Key: sculpture Values: 27
Key: sculpture. Values: 27
Key: series Values: 20 24 36 40
Key: session Values: 20 22 36 38
Key: session, Values: 3 7 27
Key: session: Values: 10 30
Key: set Values: 22 38
Key: sign Values: 24 40
Key: sketches Values: 3 7
Key: sketches. Values: 27
Key: some Values: 10 30
Key: something Values: 14
Key: stream Values: 22 38
Key: streaming Values: 22 38
Key: streams! Values: 24 40
Key: such Values: 3 7
Key: that Values: 24 40
Key: the Values: 3 7 10 20 22 24 27 30 36 38 40
Key: this Values: 3 7 10 20 24 27 30 36 40
Key: to Values: 3 7 10 12 14 20 27 30 36
Key: tool Values: 3 7
Key: trigger Values: 12
Key: triggered Values: 7
Key: until Values: 22 38
Key: up Values: 20 24 36 40
Key: up, Values: 42
Key: using Values: 3 7 27
Key: very Values: 18
Key: video Values: 27 34
Key: videos Values: 27
Key: visualizations Values: 7
Key: want Values: 24 40
Key: was Values: 24 40
Key: watch Values: 20 36
Key: we Values: 3 7 10 24 27 30 40
Key: web Values: 3 7 27
Key: weΓÇÖll Values: 22 27 38
Key: what Values: 27
Key: when Values: 14
Key: will Values: 3 7 27
Key: with Values: 2 3 7 24 40
Key: work Values: 3 7 10 27 30
Key: works Values: 3 7
Key: you Values: 20 24 36 40

注意:我不小心在第一行输入了一个输入,所以值在后面一个

【讨论】:

【参考方案2】:

我相信这行得通:

#include <iostream>
#include <string>
#include <sstream>
#include <map>
#include <set>

const char* text = 
"yellow red blue\n"
"blue blue\n"
"\n"
"red yellow blue\n"
"red blue\n";

int main()

    std::map<std::string, std::set<size_t>> words;
    
    std::stringstream ss(text); // or replace by a std::ifstream
    std::string line;
    for (size_t l0; std::getline(ss, line); l++) 
        std::stringstream lineStream (line);
        for (std::string word; std::getline(lineStream, word, ' '); ) 
            // probably some work to do on 'word' here.
            // e.g. what happens with "blue.", "Blue" or "blueblue"? 
            words[word].insert(l);
        
    

    for (auto const& kv: words) 
        std::cout << "> " << kv.first << ": ";
        for (auto const& w: kv.second) std::cout << w << " ";
        std::cout << std::endl;
    

https://godbolt.org/z/4b6Mvn

输出:

> blue: 0 1 3 4 
> red: 0 3 4 
> yellow: 0 3

【讨论】:

您能否以文本形式详细说明错误是什么以及您修复了什么,而不是给出一段代码作为答案? 我的意思是,如果 OP 有问题,我可以详细说明,但他们只是问“如何做 X”而不是“我不明白 X 是如何工作的”。 OP 想要什么是无关紧要的。这个网站是一个问答网站。 Q 和 A 只有作为问题和答案才有价值 :-) 有时 8 行代码和几个 cmets 就足够了。 “展示,不告诉”。 例如,您可以发布自己的答案,而不是光顾我如何回答。 :-)

以上是关于如何将文件中的不同行号存储到一个集合中?的主要内容,如果未能解决你的问题,请参考以下文章

如何将自定义对象的集合存储到 user.config 文件中?

Java 集合类

8.0Java集合类

如何以相反的顺序将文档从一个集合添加到另一个集合?

我如何实现这种布局,我只需要为偶数行号应用 Y 偏移量?

集合类   day-11.3