Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/powerbi/2.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++_String_Vector_Char - Fatal编程技术网

C++ 排序字符向量

C++ 排序字符向量,c++,string,vector,char,C++,String,Vector,Char,我正在尝试将输入的字符串转换为Cstring中的向量,并对它们进行排序。除了创建字符串时插入的最后一个单词被重复之外,我所有的工作都正常。我知道它就在这附近 while(getline((text), words, ' ')) spliter.push_back(words.c_str()); 我的代码出错了,所以我想知道是否有人能告诉我哪里出了问题,并能修复它。我知道一个规则的C++字符串会更有效,但是必须这样做,而我;我有点迷路了 { const int limit =301;

我正在尝试将输入的字符串转换为Cstring中的向量,并对它们进行排序。除了创建字符串时插入的最后一个单词被重复之外,我所有的工作都正常。我知道它就在这附近

while(getline((text), words, ' '))
    spliter.push_back(words.c_str());
我的代码出错了,所以我想知道是否有人能告诉我哪里出了问题,并能修复它。我知道一个规则的C++字符串会更有效,但是必须这样做,而我;我有点迷路了

{ const int limit  =301;

 char input[limit];
string words;

 characters

cin.getline(input, limit);


words =input;

vector <const char*> spliter;

stringstream text(words);

 while(getline((text), words, ' '))

        spliter.push_back(words.c_str());

        for(auto x:spliter) cout << x << " "; cout << endl ;

    auto cstr_compare = [](const char* s1, const char* s2) {

        return strcmp(s1,s2) < 0; };

    sort(spliter.begin(), spliter.end(), cstr_compare);

    for(auto x:spliter) cout << x << " "; cout << endl;

}
{const int limit=301;
字符输入[限制];
字符串;
人物
cin.getline(输入,限制);
单词=输入;
矢量分配器;
字符串流文本(字);
while(getline((文本),words,))
spliter.push_back(words.c_str());

对于(自动x:spliter)cout我可以向您推荐此解决方案吗

#include <iostream>
#include <algorithm>
#include <vector>
#include <sstream>
#include <cstring>

using namespace std;

void ctokenize(const stringstream& text, vector<const char*>& tokens)
{
    std::string::size_type pos = 0;
    std::string::size_type lastPos = 0;

    string str = text.str();

    while(std::string::npos != pos) {
        // find separator
        pos = str.find(" ", lastPos);

        // Found a token, add it to the vector.
        string strFound = str.substr(lastPos, pos - lastPos);
        tokens.push_back(strdup(strFound.c_str()));

        // set lastpos
        lastPos = pos + 1;
    }
}

int main()
{
    const int limit = 301;
    char input[limit];
    string words;

    //characters

    cin.getline(input, limit);

    words = input;

    vector<const char*> spliter;

    stringstream text(words);

    /*
    while(getline((text), words, ' ')) {
        spliter.push_back(words.c_str());
    }
    */

    ctokenize(text, spliter);

    for(auto x:spliter) {
        cout << x << " ";
    }
    cout << endl;

    auto cstr_compare = [](const char* s1, const char* s2) {
        return strcmp(s1,s2) < 0;
    };

    sort(spliter.begin(), spliter.end(), cstr_compare);

    for(auto x:spliter) {
        cout << x << " ";
    }
    cout << endl;

    return 0;
}
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
void ctokenize(常量字符串流和文本、向量和标记)
{
std::string::size\u type pos=0;
std::string::size\u type lastPos=0;
string str=text.str();
while(std::string::npos!=pos){
//查找分隔符
pos=str.find(“,lastPos”);
//找到一个标记,将其添加到向量。
字符串strFound=str.substr(lastPos,pos-lastPos);
tokens.push_back(strdup(strFound.c_str());
//设置最后位置
lastPos=pos+1;
}
}
int main()
{
常数整数极限=301;
字符输入[限制];
字符串;
//人物
cin.getline(输入,限制);
单词=输入;
矢量分配器;
字符串流文本(字);
/*
while(getline((文本),words,)){
spliter.push_back(words.c_str());
}
*/
ctokenize(文本,拆分器);
用于(自动x:拆分器){

你为什么要在向量中使用
char*
而不是
std::vector
?你不需要提供自己的比较函数。首先,没有“char vector”在你展示的代码中,有一个指向
char
的指针向量。其次,为什么你有一个指针向量而不是
std::string
的向量?你应该使用正确的缩进。我读代码有点难……也许
char
的向量更适合用例?代码是格式化的乱七八糟的小猪,很难跟上。