set:如何列出以给定字符串开头并以“/”结尾的非字符串?
例如,我们的集合中有:
bin/obj/Debug/CloudServerPrototype/ra.write.1.tlog
bin/obj/Debug/CloudServerPrototype/rc.write.1.tlog
bin/obj/Debug/vc100.idb
bin/obj/Debug/vc100.pdb
所以这就是我基于此尝试的 给出答案:
#include <iostream>
#include <algorithm>
#include <set>
#include <string>
#include <iterator>
using namespace std;
struct get_pertinent_part
{
const std::string given_string;
get_pertinent_part(const std::string& s)
:given_string(s)
{
}
std::string operator()(const std::string& s)
{
std::string::size_type first = 0;
if (s.find(given_string) == 0)
{
first = given_string.length() + 1;
}
std::string::size_type count = std::string::npos;
std::string::size_type pos = s.find_last_of("/");
if (pos != std::string::npos && pos > first)
{
count = pos + 1 - first;
}
return s.substr(first, count);
}
};
void directory_listning_without_directories_demo()
{
set<string> output;
set<string> demo_set;
demo_set.insert("file1");
demo_set.insert("file2");
demo_set.insert("folder/file1");
demo_set.insert("folder/file2");
demo_set.insert("folder/folder/file1");
demo_set.insert("folder/folder/file2");
demo_set.insert("bin/obj/Debug/CloudServerPrototype/ra.write.1.tlog");
demo_set.insert("bin/obj/Debug/CloudServerPrototype/rc.write.1.tlog");
demo_set.insert("bin/obj/Debug/vc100.idb");
demo_set.insert("bin/obj/Debug/vc100.pdb");
std::transform(demo_set.begin(),
demo_set.end(),
std::inserter(output, output.end()),
get_pertinent_part("bin/obj/Debug/"));
std::copy(output.begin(),
output.end(),
std::ostream_iterator<std::string>(std::cout, "\n"));
}
int main()
{
directory_listning_without_directories_demo();
cin.get();
return 0;
}
输出:
CloudServerPrototype/
file1
file2
folder/
folder/folder/
vc100.idb
vc100.pdb
并且我们得到了 bin/obj/Debug/
string。我们想知道:
vc100.idb
vc100.pdb
CloudServerPrototype/
这样的事情怎么办?
for example we have in our set:
bin/obj/Debug/CloudServerPrototype/ra.write.1.tlog
bin/obj/Debug/CloudServerPrototype/rc.write.1.tlog
bin/obj/Debug/vc100.idb
bin/obj/Debug/vc100.pdb
So this is what I tried based on this grate answer:
#include <iostream>
#include <algorithm>
#include <set>
#include <string>
#include <iterator>
using namespace std;
struct get_pertinent_part
{
const std::string given_string;
get_pertinent_part(const std::string& s)
:given_string(s)
{
}
std::string operator()(const std::string& s)
{
std::string::size_type first = 0;
if (s.find(given_string) == 0)
{
first = given_string.length() + 1;
}
std::string::size_type count = std::string::npos;
std::string::size_type pos = s.find_last_of("/");
if (pos != std::string::npos && pos > first)
{
count = pos + 1 - first;
}
return s.substr(first, count);
}
};
void directory_listning_without_directories_demo()
{
set<string> output;
set<string> demo_set;
demo_set.insert("file1");
demo_set.insert("file2");
demo_set.insert("folder/file1");
demo_set.insert("folder/file2");
demo_set.insert("folder/folder/file1");
demo_set.insert("folder/folder/file2");
demo_set.insert("bin/obj/Debug/CloudServerPrototype/ra.write.1.tlog");
demo_set.insert("bin/obj/Debug/CloudServerPrototype/rc.write.1.tlog");
demo_set.insert("bin/obj/Debug/vc100.idb");
demo_set.insert("bin/obj/Debug/vc100.pdb");
std::transform(demo_set.begin(),
demo_set.end(),
std::inserter(output, output.end()),
get_pertinent_part("bin/obj/Debug/"));
std::copy(output.begin(),
output.end(),
std::ostream_iterator<std::string>(std::cout, "\n"));
}
int main()
{
directory_listning_without_directories_demo();
cin.get();
return 0;
}
This outputs:
CloudServerPrototype/
file1
file2
folder/
folder/folder/
vc100.idb
vc100.pdb
and we are given with bin/obj/Debug/
string. We want to cout:
vc100.idb
vc100.pdb
CloudServerPrototype/
How to do such thing?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您想要做什么的快速示例。
String.find(): http://www.cplusplus.com/reference/string/ string/find/
String.subStr(): http://www.cplusplus.com/reference/string/string/substr/
Quick example of what you want to do.
String.find(): http://www.cplusplus.com/reference/string/string/find/
String.subStr(): http://www.cplusplus.com/reference/string/string/substr/
目前尚不清楚您遇到了问题的哪一部分,因此这里有一个入门指南。
要获取“给定字符串”和最终“/”(如果存在)之间的字符串部分:
要将这些部分插入到新集合(
输出
)中以保证唯一性,您可以使用以下命令:您可能希望将
given_string
传递给get_pertinent_part()
,在这种情况下,您需要将其转换为函子:然后您可以这样调用它
:新的
set
:对结果进行排序留作练习。
It's not clear with which part of the problem you are stuck, so here is a starter for you.
To get the parts of the strings between "given string" and the final '/' (where present):
To insert these parts into a new set (
output
) to guarantee uniqueness you can use the following:You may wish to pass
given_string
intoget_pertinent_part()
, in which case you'll need to convert it to a functor:You can then call it this way:
To output the new
set
:Sorting the results is left as an exercise.
我能想到的最简单的方法,使用标准 C 函数,是:
首先发生的事情是在我们正在分析的字符串中查找子字符串
string1
,即string2< /代码>。一旦我们找到起点,并假设它在那里,我们使用指针算术将该子字符串的长度添加到该起点,然后通过从结束位置减去起始位置来找到结果字符串的长度,这是找到的与
strchr(position, '/')
。然后我们只需将该子字符串复制到缓冲区中,然后使用 cout 进行打印。我确信有一种奇特的方法可以用
std::string
来做到这一点,但我会把它留给任何能够更好地解释 C++ 字符串的人,我从来没有设法适应它们,哈哈The easiest way I can think of, using the standard C functions, would be:
The first thing that occurs, is finding the substring,
string1
, in the string we are analyzing, beingstring2
. Once we found the starting point, and assuming it was there at all, we add the length of that substring to that starting point using pointer arithmatic, and then find the resulting string's length by subtracting the starting position from the ending position, which is found withstrchr(position, '/')
. Then we simply copy that substring into a buffer and it's there to print with cout.I am sure there is a fancy way of doing this with
std::string
, but I'll leave that to anyone who can better explain c++ strings, I never did manage to get comfortable with them, haha