根据string特性:
1. size_t find (const string& str, size_t pos = 0)
str.find(str1)
说明:从pos(默认是是0,即从头开始查找)开始查找,找到第一个和str1相匹配的子串,返回该子串的起始索引位置;如果没有找到则返回string::npos
参考:find函数:http://www.cplusplus.com/reference/string/string/find/
2. size_t find_first_of (const string& str, size_t pos = 0)
str.find_first_of(str1)
说明:从pos(默认是是0,即从头开始查找)开始查找,找到第一个和str1相匹配的子串,返回该子串的起始索引位置;如果没有找到则返回string::npos
参考:find_first_of函数:http://www.cplusplus.com/reference/string/string/find_first_of/
3.size_t find_last_of (const string& str, size_t pos = npos)
str.find_last_of(str1)
说明:从npos(默认是字符串最后一个,即从后向前查找)开始查找,找到第一个和str1相匹配的子串,返回该子串的最后一个字符的索引位置;如果没有找到则返回string::npos
参考: find_last_of函数:http://www.cplusplus.com/reference/string/string/find_last_of/
遍历文件去掉"." ".." 隐藏文件 :程序
#include <stdio.h>
#include <sys/types.h>
#include <dirent.h>
#include <iostream>
#include <string.h>
int main()
{
DIR* pDir;
struct dirent* ptr;
if( !(pDir = opendir("./")) )
return -1;
while( (ptr = readdir(pDir)) != 0 )
{
std::string s = ptr->d_name;
if(((std::string)ptr->d_name).find_first_of("."))
continue;
/*Processing File*/
std::cout << "file name=" << ptr->d_name
<< ", ino=" << ptr->d_ino
<< ", reclen=" << ptr->d_reclen
<< std::endl;
}
closedir(pDir);
return 0;
}