如何找到字符串中字符的位置?前任。如果输入“ abc * ab”,我想仅用“ abc”创建一个新字符串。你能帮我解决我的问题吗?
std::find
将迭代器返回到找到的第一个元素,该迭代器与您要查找的元素进行比较(如果找不到第二个参数,则返回第二个参数,在本例中为结束迭代器。)您可以构造一个std::string
使用迭代器。
#include <iostream>
#include <string>
#include <algorithm>
int main()
{
std::string s = "abc*ab";
std::string s2(s.begin(), std::find(s.begin(), s.end(), '*'));
std::cout << s2;
return 0;
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句