简体   繁体   中英

map.insert of * argv[]

I have some problem to understand which parameters would be the right once at the insert function of my <map> , maybe you can help me and explaine why?

I wanna open a file, and also save the name of the file which is given by the user over the arguments. To do this I thought it is a good soultion to make a map with a ifsteam, and a string object.

Here is the code:

 int main(int argc, char** argv)
    {
        std::map<std::ifstream*, std::string> Dateien;
        auto it_dateien = Dateien.begin();
    
        for(size_t param = 1; param < argc; param++)
        {
            //No valid instance of the constructor
            Dateien.insert(it_dateien,std::pair<std::ifstream*, std::string((*argv[],*argv[]));

Thanks in advance!

If I understand correctly what you want to achieve you need something like this:

int main(int argc, char** argv)
{
    std::map<std::string, std::ifstream> Dateien;

    for (size_t param = 1; param < argc; param++)
    {
        Dateien[argv[param]] = std::ifstream{ argv[param] };
    }
}

I assume that you want to save in a map the name of the file you want to read, and to retreive the corresponding ifstream by doing Datein.at(NameOfFile) .

For example the usage can be something like this:

std::string line;
auto& firstFile = Dateien.at(argv[1]);
if (firstFile.is_open())
{
    while (std::getline(firstFile, line))
    {
        std::cout << line << '\n';
    }
    firstFile.close();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM