[英]Find letters from a text file
所以我是 C++ 的新手,我在使用程序時遇到了問題。 我試圖搜索一個文件,然后找到所有 26 個大寫字母(A、B、C...)以及小寫字母(a、b、c、d...)的第一次出現。 我一直在使用代碼並認為最簡單的方法是獲取文件並將其創建為向量,然后遍歷向量並找到每個字母的第一個實例。 這是我的代碼示例。
int main()
{
int i;
string file = input; // User inputted file
vector<string> v;
ifstream ist{ file };
if (!ist)
error("Can not open inputed file ", file);
while (!ist.eof())
{
string x;
ist >> x;
v.push_back(x); // Creates a string vector that is filled with everything
// in the file
}
// Find A in text.
vector<int> location;
location = find(v.begin(), v.end(), 'A');
if (location != v.end())
cout << "Found A at location " << location;
else
cout << "A was not found"
我能夠成功檢索向量 v 並且它充滿了文件內部的內容。 問題在於如何從字符串向量中獲取字母的位置。 我仍然需要 C++,所以我可能會完全錯誤地解決問題。 如果你能幫助我,那就太棒了。 謝謝。
在您搜索字母時,您的向量 v 包含文件中的所有字符串。
制作 v vector<char>
並按一個字符讀取
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.