Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/163.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++用第一个和最后一个字母找出单词,按字母顺序排序。_C++_Class_Sorting_Object - Fatal编程技术网

C++用第一个和最后一个字母找出单词,按字母顺序排序。

C++用第一个和最后一个字母找出单词,按字母顺序排序。,c++,class,sorting,object,C++,Class,Sorting,Object,我不能让程序读取文件中的单词,因为它们的首字母和末字母没有限制。我使用类和对象,首先我不能阅读它们 #include <iostream> #include <string> #include <sstream> #include <fstream> using namespace std; class Texts{ public: void Realding(string &All); void Searching(s

我不能让程序读取文件中的单词,因为它们的首字母和末字母没有限制。我使用类和对象,首先我不能阅读它们

#include <iostream>
#include <string>
#include <sstream>
#include <fstream>

using namespace std;
class Texts{

public:
    void Realding(string &All);
    void Searching(string &text, char *Word);
};
int main()
{

    Texts A;
    string Text; char word[40];
    A.Reading(Text);
    A.Searching(Text, word);
    system("PAUSE");


}

void Texts::Reading(string &All)
{
    string temp;
    ifstream read("Text.txt");
    while (getline(read, temp)) { All += temp; All += "\n"; }
    cout << All;

}

void Texts::Searching(string &text, char *Word)
{
    int i = 0;
    int j = 0;
    int letters = 0;
    int zodz = 0;
    int index = 0;
    while (1)
    {

        if (text[i] == ' ' || text[i] == '\0')
        {

            zodz++;
            if (text[0] == text[i - 1])
            {
                letters = j;
                for (int l = 0; l < letters; l++)
                {
                    //cout << text[l];
                }
                j = 0;
            }
            if (text[i + 1 - j] == text[i - 1])
            {
                letters = j;
                for (int l = i - j; l < letters; l++)
                {
                //  cout << text[l];
                }
                j = 0;
            }


        }

        if (text[i] == '\0') break;   
        else
        i++;                           
        j++;

    }
}
以及如何在以后选择第一个和最后一个字母相同的单词分配给数组,即以后如何按字母顺序排序。如果有人帮助我,谢谢。

阅读文件:

std::ifstream in(NameOfFile);
std::string word;

while (in >> word) //will stop when word can't be read. probably bad file or end of file
{
    // do something with word
}
查找首字母和末字母相同的单词

if (word.front() == word.back())
{
    // do something with word
}
注意:这不处理大写字母的单词。它找不到妈妈。它很可能会因为空话而崩溃。两者都有微不足道的修正

将字分配给数组

array[index++] == word;
这假设您希望在插入到数组中后推进索引。请注意,如果数组填充过多,程序将表现不佳。如果您被允许在作业中使用,请考虑使用STD::vector。< /P> 排序数组

std::sort(array, array + index);

这假定允许您使用std::sort。同样,如果可能,使用std::vector代替数组。假设index是在完成所有添加之后,上面添加示例中的index的值。如果不允许使用std::sort,请询问另一个问题。这是一个冗长的主题。

是否有使用类的要求?我建议您在主功能或其他独立功能中执行此操作。它可以帮你解决这个问题;我怀疑这是你真正的密码。如果你不向我们展示你的真实代码,我们很难帮助你;不要把它们都读入一个字符串,这是有要求的。哦,对不起,是的,这里有蚂蚁搜索功能。这是真正的代码。如果word.front==word.back可能更清晰一些,我将更改函数。
std::sort(array, array + index);