Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/125.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 - Fatal编程技术网

C++ 重新组织向量的内容

C++ 重新组织向量的内容,c++,string,vector,C++,String,Vector,我想重新组织向量的内容,以从以下内容开始,例如: // 'v' is the vector v[0]: "name: " v[1]: & v[2]: "James" v[3]: & v[4]: 10 v[5]: & v[6]: "last name: " v[7]: & v[8]: "Smith" 然后说: v[0]: "name: " & "James" & 10 & "last name: " & "Smith" 这个例子

我想重新组织向量的内容,以从以下内容开始,例如:

// 'v' is the vector

v[0]: "name: "
v[1]: &
v[2]: "James"
v[3]: &
v[4]: 10
v[5]: &
v[6]: "last name: "
v[7]: &
v[8]: "Smith"
然后说:

v[0]: "name: " & "James" & 10 & "last name: " & "Smith"
这个例子非常基本

我试过这样的方法:

std::vector<std::string> organize(std::vector<std::string> tokens) {
        using namespace std;
        vector<string> ret;
        string tmp;
        // The IsString function checks whether a string is enclosed in quotation marks.
        // The s_remove_space function removes spaces from a character string.
        for (string str : tokens) {
            if (IsString(str) || str == tokens[tokens.size() - 1]) {
                ret.push_back(tmp);
                ret.push_back(str);
                tmp.clear();
            }
            else if (s_remove_space(str) != "")
                tmp += str;
        }
        return ret;
    }
std::vector组织(std::vector标记){
使用名称空间std;
向量ret;
串tmp;
//IsString函数检查字符串是否包含在引号中。
//s_remove_space函数从字符串中删除空格。
for(字符串str:tokens){
if(IsString(str)| | str==tokens[tokens.size()-1]){
后推(tmp);
后推回(str);
tmp.clear();
}
否则如果(s_删除空间(str)!=“”)
tmp+=str;
}
返回ret;
}
如果我们从上面的例子来看,输出和输入是一样的。 此外,我做事的方式似乎相当残忍。我想用RegEx系统实现起来很简单,但我不能/不想使用它们

在我的项目上一步一步地调试VC++并不能帮助我解决这个问题。在我看来,这个问题很容易解决


在我看来,这个错误是愚蠢的,但我已经找了很长一段时间了。

简单是一种美德,不要把事情复杂化

这里是一个简单的示例,您只需迭代每个标记,然后将其附加到向量的第一个字符串。特殊情况是第一个和最后一个标记,您应该分别附加一个空格和一个标记:

#include <iostream>
#include <vector>
#include <string>

using namespace std;

int main() {
    vector<string> v = {"\"name: \"", "&", "\"James: \"", "&", "10", "&", "\"last name: \"", "&", "\"Smith\""};
    for(unsigned int i = 0; i < v.size(); ++i) {
        if(i == 0)
            v[0] += " ";
        else if(i == v.size() - 1)
            v[0] += v[i];
        else
            v[0] += v[i] + " ";
    }
    cout << "v[0]: " << v[0] << endl;
}
#包括
#包括
#包括
使用名称空间std;
int main(){
向量v={“\”姓名:\”、“&”、“\”詹姆斯:\”、“&”、“10”、“&”、“\”姓氏:\”、“&”、“\”史密斯\\”;
for(无符号整数i=0;icout在这种情况下,
std::stringstream
对于收集结果字符串更有用

如果不需要定位,也可以使用
的范围

#include <iostream>
#include <vector>
#include <string>
#include <sstream>
#include <numeric>

std::vector< std::string >& organize( std::vector< std::string >& v )
{
    std::stringstream result;
    //bool is_first = true;
    //char seperator = ' ';

    for ( const auto& str : v )
    {
        //if (is_first)
        //    is_first = false;
        //else
        //    result << seperator;

        result << str;
    }

    v[ 0 ] = result.str();

    return v;
}

int main()
{
    std::vector< std::string > v = {
          "\"name: \"", "&", "\"James: \"", "&", "10", "&", "\"last name: \"", "&", "\"Smith\""};

    organize( v );

    std::cout << "v[0]: " << v[ 0 ] << std::endl;
}
#包括
#包括
#包括
#包括
#包括
std::vector&组织(std::vector&v)
{
std::stringstream结果;
//bool is_first=真;
//字符分隔符=“”;
用于(常量自动和str:v)
{
//如果(你是第一个)
//is_first=false;
//否则

//结果为什么在你的例子中你没有删除空格,但是在你的代码中你调用了
s\u remove\u space
?什么是
&
?你的
向量只能保存字符串,不能
&
s。你的意思是
“&”
v[0]:“name:”和“James”&10&“last name:”和“Smith”
实际上应该是
v[0]:“name:&James&10&last name:&Smith”
?在我的项目中一步一步地调试VC++并不能帮助我解决问题。--我不理解这句话。你在编写代码时考虑了一个计划(或在纸上),您应该能够逐步浏览您的程序,以查看程序与您的计划之间的差异。您还应该发布一个,因为最后一个注释是正确的,因为由于伪代码,我们不知道该向量中实际包含什么。请为您的示例提供一些测试代码,以便我们能够更好地理解。@nwp:&'也是一个字符串。我这样做了不要在上面加引号,这样我就不会“加重负担”,并且有一个更清晰的愿景。事实上,这很简单!非常感谢,你给了我其他改进我的计划的想法。