首页 > 社交 > 科普中国

C++

常驻编辑 科普中国 2022-07-18 标准   特性   定义   概念   类型
gV0拜客生活常识网

#include 
#include 
int main()
{
    std::filesystem::path folderpath = "C:MyFolder";
    if (std::filesystem::exists(folderpath))
    {
        std::cout << "The path: " << folderpath << " exists.";
    }
    else
    {
        std::cout << "The path: " << folderpath << " does not exist.";
    }
}

Similarly, we can use the std::filesystem::path object to check if a file exists:gV0拜客生活常识网

#include 
#include 
int main()
{
    std::filesystem::path folderpath = "C:MyFoldermyfile.txt";
    if (std::filesystem::exists(folderpath))
    {
        std::cout << "The file: " << folderpath << " exists.";
    }
    else
    {
        std::cout << "The file: " << folderpath << " does not exist.";
    }
}

To iterate over folder elements, we use the std::filesystem::directory_iterator iterator:gV0拜客生活常识网

#include 
#include 
int main()
{
    auto myfolder = "C:MyFolder";
    for (auto el : std::filesystem::directory_iterator(myfolder))
    {
        std::cout << el.path() << '
';
    }
}

Here we iterate over the directory entries and print each of the elements full path using the .path() member function.gV0拜客生活常识网

For Linux, we need to adjust the path and use the following instead:gV0拜客生活常识网

#include 
#include 
int main()
{
    auto myfolder = "MyFolder/";
    for (auto el : std::filesystem::recursive_directory_iterator(myfolder))
    {
        std::cout << el.path() << '
';
    }
}

To iterate over folder elements recursively, we use the std::filesystem::recursive_directory_iterator. This allows us to iterate recursively over all subfolders in a folder. On Windows, we would use:gV0拜客生活常识网

#include 
#include 
int main()
{
    auto myfolder = "C:MyFolder";
    for (auto el : std::filesystem::recursive_directory_iterator(myfolder))
    {
        std::cout << el.path() << '
';
    }
}

On Linux and similar OS-es, we would use the following path:gV0拜客生活常识网

#include 
#include 
int main()
{
    auto myfolder = "MyFolder/";
    for (auto el : std::filesystem::directory_iterator(myfolder))
    {
        std::cout << el.path() << '
';
    }
}

Below are some useful utility functions inside the std::filesystem namespace:gV0拜客生活常识网

std::filesystem::create_directory for creating a directorygV0拜客生活常识网

std::filesystem::copy for copying files and directoriesgV0拜客生活常识网

std::filesystem::remove for removing a file or an empty foldergV0拜客生活常识网

std::filesystem::remove_all for removing folders and subfoldersgV0拜客生活常识网

17.5 std::string_view

引用一段字符串并保留长度信息。gV0拜客生活常识网

Copying data can be an expensive operation in terms of CPU usage. Passing substrings as function parameters would require making a copy of substrings. This is a costly operation. The string_view class template is an attempt to rectify that.gV0拜客生活常识网

The string_view is a non-owning view of a string or a substring. It is a reference to something that is already there in the memory. It is implemented as a pointer to some character sequence plus the size of that sequence. With this kind of structure, we can parse strings efficiently.gV0拜客生活常识网

The std::string_view is declared inside the header file. To create a string_view from an existing string, we write:gV0拜客生活常识网

#include 
#include 
#include 
int main()
{
    std::string s = "Hello World.";
    std::string_view sw(s);
    std::cout << sw;
}

相关阅读:

  • 巩俐谈北影节评奖标准,陈坤请教好友周迅,张颂文笑言可能
  • pm2.5标准范围多少正常
  • 安徽最低工资标准2021上调了吗
  • 正常血压标准范围是多少(血压高到160危险吗)
  • 血红蛋白正常值是多少(3到6岁血红蛋白标准)
  • 视力正常标准是多少(视力最好是5.0还是5.2)
  • 甲醛标准范围多少正常(甲醛0.11宝宝能住吗)
  • 一海里等于多少米(标准海里长度为多少米)
  • 酒驾标准是多少毫升(酒驾吹气低于20怎么处理)
  • 国道限速多少(国道限速标准)
    • 网站地图 |
    • 声明:登载此文出于传递更多信息之目的,并不意味着赞同其观点或证实其描述。文章内容仅供参考,不做权威认证,如若验证其真实性,请咨询相关权威专业人士。