Programming learning tool articles --boost

foreword

        I have been in contact with it since 2018 boost, but I have never understood its functions in detail. Due to work needs, I looked at it in the past few days and boostfound that it is indeed very powerful. I am really ashamed of my previous ignorance. boostIt contains too much content, and the data structure , algorithms, timing, etc., if there is no specific usage requirements, it is difficult to master, so start with simple and commonly used things.
       The core value of learning boostis to use it better C/C++, and use it C/C++as a standard extension. If you encounter repetitive functions and the operating modules are relatively independent, you will try to choose the one with the largest function as the foothold of the function.
       winDownload the full-featured version of the compilation boost. 1GIn actual use, if you think it is too large, you can perform a certain degree of castration. The specific implementation method can be downloaded from Baidu.

1. Introduction to Filesystem

       As boosta beginning, filesystemrecord the use of the module, because the main function of the module is to deal filewith and Directoriesfill in C/C++the gaps, and also solve the cross-platform barriers. At the same time, it is also considered that the module is relatively widely used and relatively independent, so first, Get this module.

2. Functional Learning

       The operation of files and folders is relatively fixed, such as creating, deleting, copying, and renaming, which is very consistent with the winoperation guide of the file manager, but many logical judgments in it are more troublesome, and the following is aimed at this Do some record learning for the application scenarios of several functions.

2.1 Create a new folder

       When creating a file/folder, a path is needed to record the location of the file/folder. Therefore, the concept of filesystemvariable is introduced in , which can be interchanged with the expression path to a certain extent . However, the function is relatively single, only indicating the path Or files under a certain path are not easy to cause misunderstanding.        First of all, one needs to be given . Of course, this can be a folder, a file, or something inexplicable:boost::filesystem::pathpathstringpath
pathpath

path p("./") //当前文件夹

       Is it std::stringa bit similar to and, in the next operation, it will be used as pan example.
       The operation of creating a new folder in 文件管理器(win)is very simple. You only need to enter the target folder step by step, and then right-click 新建. But you need to be careful when using code. After all 人非圣贤,孰能无过, who knows whether the parameters you give are correct. For example, this p, is just a string, it is difficult for the program to know what it means specifically, if an illegal operation occurs, it is likely to cause the program to crash directly, so after getting it, it is first necessary to distinguish it, determine pwhether it exists, and judge other properties etc.:

if (exists(p)) {
	if (is_regular_file(p))
		LOG(INFO) << p << "size is " << file_size(p);
	else if (is_directory(p)) 
		LOG(INFO) << p << "is a directory , size is ";
	else
		LOG(WARNING) << "don't know";
}
else {
	LOG(INFO) << p << " do not exist";
	create_directory(p);
}

       The above is just a simple operation instruction, and the focus is on logical judgment. Through multiple if(but not used to switch) settings and these few lines of code, some small functions can be realized, but different application scenarios will appear. The combination method will be introduced below.
       There are two main commands to create a new folder:

boost::filesystem::create_directories(path p);
boost::filesystem::create_directory(path p);

       The main difference between the two commands is that create_directories()it can be created in multiple layers. Simply put, if p = "./123/456"it does not exist 123, the command can create a folder first 123and then create a folder 456. However, if you use create_directory()the command to create it without 123it 456, The program just crashes.

2.2 Delete files (folders)

       There are also two commands to delete files (folders):

remove(path p)
remove_all(path p)

        The main difference: remove()it can only be used to delete a file or folder. When there are other files in the provided folder, this command can cause the program to crash.

2.3 Rename and copy

       Relatively speaking, it is much simpler, with a total of 3 commands:

rename(old, new);
copy_file(old, new);
copy_directory(old, new);

       Here, there are a few points to note:

  • It is necessary to pay attention to the problem of duplicate names. When renameupdating copy, if the new name has the same name, the program will hang directly, which is winthe same as the following operation logic.
  • copy_fileCan only copy files, used to copy folders directly hang up
  • copy_directory()Can only copy folders, however, is not responsible for copying the contents of folders.

3. Application scenarios

       Scenario 1 : During data processing, it is necessary to save the variables of the intermediate process, and it is also convenient for reference;
       solution idea : create a series of folders based on the system time, and save the corresponding data to the folder at the corresponding time. Use 年月日as a first-level directory, and use 时分秒as a second-level directory, and store data in it.

std::string path_f = "./";
std::string path_s = "";
boost::posix_time::ptime now_data = second_clock::local_time();
auto now_time = now_data.time_of_day();
//git current time as folder names
path_f = path_f + to_string(now_data.date().year()) + “-” 
					   + to_string(now_data .date().month()) + "-" 
					   + to_string(now_data .date().day());
path_s =  to_string(now_time.hours()) + “-” +
			    to_string(now_time.minutes()) + "-" +
			    to_string(now_time.seconds);
path p = path_f + "/" + path_s;
//create directories
create_directories(p);

       The above is created by create_directories()creating, the disadvantage is that it is too smart, no matter whether there is a target folder or not, it will be created by itself, the process is not easy to control, if you want to use create_directory()create a folder, you can refer to the first example in this article.

       Scenario 2 : When creating folders, you want to 0,1,2,3...name them sequentially;
       solution idea : first traverse the number of files in the target folder, and then perform +1operations; at the same time, you can also count the number of files and folders according to your needs :

int count = 0;
for (auto it : directory_iterator(p))
		count ++;

       Scenario 3 : Given a path p, get the names of all the files contained in it;
       solution idea : a recursive solution

bool get_files(path p, std::vector<path>& names)
{
	if (exists(p)) {
		if (is_directory(p)) {
			for (auto it : directory_iterator(p)) {
				if (is_regular_file(it))
					names.push_back(it);
				else if (is_directory(it))
					get_files(it, names);
			}
		} else 
			names.push_back(p);
	}
	else
		return false;
}
std::vector<path> names;
bool result = get_files(p, names);

Guess you like

Origin blog.csdn.net/weixin_42823098/article/details/115591080