微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

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

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

func

解决方法

这是我的解决方案:

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;
    }

}

评论几乎解释了我的所作所为。

我使用从 https://stackoverflow.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,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,we’ll be streaming a new session at 4PM EST. You can set a reminder for the stream for April 13th here.

Finally,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

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

,

我相信这有效:

#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 l{0}; 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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。