📄 words_search_2.cpp
字号:
/*
* Description:
*
* Report the position of a word in a sentence.
*
* History:
*
* Initial version created by Royal, Mar. 2004.
*
* Notes:
*
* This code has been written to conform to standard C++ and STL. It has been
* compiled successfully using GNU C++ 3.2, Borland C++ 5.5, and Visual C++ 7.0.
*/
#include <iostream>
#include <iomanip>
#include <vector>
#include <string>
using namespace std;
vector<int> search(const vector<string>& sentence, const string& word);
int main()
{
string input;
vector<string> sentence;
cout << "Please input a sentence:" << endl;
while(cin >> input) sentence.push_back(input);
cin.clear();
string word;
cout << "Please input the word to be searched: ";
cin >> word;
vector<int> pos = search(sentence, word);
if (!pos.empty())
{
cout << "Find the word at the following position:";
for(vector<int>::const_iterator it = pos.begin(); it != pos.end(); ++it)
cout << setw(3) << (*it+1);
}
else cout << "Can't find the word." << endl;
}
vector<int> search(const vector<string>& sentence, const string& word)
{
vector<int> pos;
vector<string>::size_type size = sentence.size();
for(vector<string>::size_type i = 0; i < size; ++i)
if(sentence[i] == word) pos.push_back(i);
return pos;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -