我如何找到給定字符串在一行中出現的第n個索引?我需要這個從該索引獲取子字符串。這是可能的通過任何函數在c + +?第n個字符串的索引
回答
有在助推find_nth
模板函數:http://www.boost.org/doc/libs/1_54_0/doc/html/boost/algorithm/find_nth.html
#include <iostream>
#include <boost/algorithm/string/find.hpp>
using namespace std;
using namespace boost;
int main() {
string a = "The rain in Spain falls mainly on the plain";
iterator_range<string::iterator> r = find_nth(a, "ain", 2);
cout << distance(a.begin(), r.begin()) << endl;
return 0;
}
您可以使用下面的函數
#include <string.h>
int strpos(char *haystack, char *needle, int nth)
{
char *res = haystack;
for(int i = 1; i <= nth; i++)
{
res = strstr(res, needle);
if (!res)
return -1;
else if(i != nth)
res = res++;
}
return res - haystack;
}
返回-1,如果它不能找到n次出現。
line「res = res ++;」似乎什麼都不做,會改變它只是「res ++;」 – Streamsoup
這個模板函數應該把工作做好
template<typename Iter>
Iter nth_occurence(Iter first, Iter last,
Iter first_, Iter last_,
unsigned nth)
{
Iter it = std::search(first, last, first_, last_);
if (nth == 0) return it;
if (it == last) return it;
return nth_occurence(it + std::distance(first_, last_), last,
first_, last_, nth -1);
}
使用
int main()
{
std::string a = "hello world world world end";
std::string b = "world";
auto it1 = nth_occurence(begin(a), end(a), begin(b), end(b), 0);
auto it2 = nth_occurence(begin(a), end(a), begin(b), end(b), 1);
auto it3 = nth_occurence(begin(a), end(a), begin(b), end(b), 2);
auto it4 = nth_occurence(begin(a), end(a), begin(b), end(b), 3);
std::cout << std::distance(begin(a), it1) << "\n";
std::cout << std::distance(begin(a), it2) << "\n";
std::cout << std::distance(begin(a), it3) << "\n";
std::cout << std::boolalpha << (it4 == end(a)) << "\n";
}
=> 6, 12, 18, true
我可否知道您使用的是哪種版本的增強版? – huahsin68
我真的很喜歡RCS的答案,做出巧妙地運用指針。我認爲,除了使用boost庫之外,它是實現OP所需結果的最簡潔的方式。然而,我在某些沒有使用指針的代碼中執行它時遇到了問題(我仍然是一個初學者),所以這裏有一個等價的答案,它不使用指針或boost庫。
int strpos(string haystack, char needle, int nth)
{// Will return position of n-th occurence of a char in a string.
string read; // A string that will contain the read part of the haystack
for (int i=1 ; i<nth+1 ; ++i)
{
std::size_t found = haystack.find(needle);
read += haystack.substr(0,found+1); // the read part of the haystack is stocked in the read string
haystack.erase(0, found+1); // remove the read part of the haystack up to the i-th needle
if (i == nth)
{
return read.size();
}
}
return -1;
}
這樣做只使用的std :: string的一個簡單的方法::找到
size_t find_nth(const string& haystack, size_t pos, const string& needle, size_t nth)
{
size_t found_pos = haystack.find(needle, pos);
if(0 == nth || string::npos == found_pos) return found_pos;
return find_nth(haystack, found_pos+1, needle, nth-1);
}
這段代碼不能很好地處理髮現特定事件的情況。就我而言,它返回了一個不合理的數字。喜歡代碼的簡單性和使用'size_t'。 –
我認爲這個大數字是'string :: npos'。如果沒有找到匹配,函數應該返回'string :: npos',就像'string :: find'一樣。 – rmorarka
這很有道理。然後我會自己處理'string :: npos'。感謝代碼片段! –
爲此,您可以使用std::string::find
並跟蹤返回的位置。在執行此操作時,您可以檢查是否找不到所需的字符串,並返回-1。
#include <string>
int nthOccurrence(const std::string& str, const std::string& findMe, int nth)
{
size_t pos = 0;
int cnt = 0;
while(cnt != nth)
{
pos+=1;
pos = str.find(findMe, pos);
if (pos == std::string::npos)
return -1;
cnt++;
}
return pos;
}
- 1. 獲取第n個索引字符串
- 2. T-SQL - 如何搜索字符串中的第n個字符
- 3. 字符串中的第N個字
- 4. 如何將字符串插入numpy列表中的每個第n個索引?
- 5. 查找字符串中字符的第一個索引
- 6. 字符串中第一個字符的T-SQL索引
- 7. 查找字符串中字符的第二個索引?
- 8. 使用tsql返回字符串中第n個元素的索引
- 9. 第n個字符
- 10. Perl替換字符串中的第n個子字符串
- 11. 查找分隔字符串中的第n個字符串
- 12. 替換字符串中出現的第n個子字符串
- 13. 替換第n個字符串
- 14. 如何grep第n個字符串
- 15. 在Swift中查找字符串中第N個子字符串實例的索引
- 16. 將字符串的第二個索引與數組字符串中的下一個字符串的第二個索引進行比較
- 17. 如何分割的python字符串中的每個第n-1 + n個字符
- 18. 提取第n個字符和另一個第n個字符之間的字符串
- 19. 在行中找到第n個字符串或子字符串
- 20. 如何獲得字符串中第一個數字的索引
- 21. Ruby:ActiveRecord - 第N行的索引
- 22. php在字符串的第n個字符上添加不同的字符串
- 23. SAS:如何在字符串中找到第n個字符/字符組的第n個實例?
- 24. 從字符串中刪除N個第一個字符
- 25. 字符串的第n個字符到int
- 26. 如何獲得字符串的第n個字符?
- 27. 在Python中分割第n個字符的字符串
- 28. 按字符串的第n個字符排序列表c#
- 29. Xquery按字符的第N個出現拆分字符串
- 30. 如何替換字符串中第n個字符的發生?
這個答案會給出最後一次出現,即25次。我可以知道如何檢索真正的第二個索引。 – huahsin68
@ huahsin68:該索引是基於零的,因爲它在find_nth文檔中陳述。只需用1找到第二個發生。 –