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

C++ 对函数中的两个向量进行编码

C++ 对函数中的两个向量进行编码,c++,c++11,vector,C++,C++11,Vector,嘿,我正在编写一个函数,它接受两个std::vector,并返回第三个std::vector 该函数将对这两个向量进行编码,并创建第三个向量 我目前正在调试它,以找出它不起作用的原因,我不断得到:向量下标超出范围。据我所知,它在这条线上崩溃了: if (file2[i].size() < file1[i].size()) 知道这里发生了什么吗?列举了一些问题: -假设file1和file2具有相同的大小,或者至少file1具有大小,但不具有==。 -您正在使用诸如file3[i][x]之

嘿,我正在编写一个函数,它接受两个
std::vector
,并返回第三个
std::vector

该函数将对这两个向量进行编码,并创建第三个向量

我目前正在调试它,以找出它不起作用的原因,我不断得到:
向量下标超出范围。据我所知,它在这条线上崩溃了:

if (file2[i].size() < file1[i].size())

知道这里发生了什么吗?

列举了一些问题:

-假设file1和file2具有相同的大小,或者至少file1具有大小,但不具有==。
-您正在使用诸如
file3[i][x]
之类的语句访问无效内存,因为
file3[i]
中的字符串初始化为空,因此不包含任何字符

这是我在不知道编码算法的确切步骤的情况下可以得到的更接近的结果

#include <iostream>
#include <vector>
#include <boost/lexical_cast.hpp>

using namespace std;

std::vector<std::string> Encode(std::vector<std::string> &file1,
    std::vector<std::string> &file2) {

    assert(file1.size() <= file2.size());
    std::vector<std::string> file3(file1.size());
    std::string temp;

    for (unsigned int i = 0; i < file1.size(); i++) {
        for (unsigned int x = 0; x < file1[i].size(); x++) {
            int enc = 0;
            if (file2[i].size() <= file1[i].size()) {
                for (unsigned int t = 0; t < file2[i].size(); t++) {
                    enc = (int)file1[i][x] + (int)file2[i][t];
                }
            }
            else if (file2[i].size() > file1[i].size()) {
                enc = (int)file1[i][x] + (int)file2[i][x];
            }

            if (enc > 126) {
                file3[i] += (enc % 127);
            }
            else {
                file3[i] += (enc + 32);
            }
        }
    }
    return file3;
}

int main(int argc, char *argv[]) {
    std::vector<std::string> a{ "1", "2", "3" };
    std::vector<std::string> b{ "6", "7", "8" };

    for (const auto& s : a)
        std::cout << s << std::endl;

    for (const auto& s : b)
        std::cout << s << std::endl;

    auto c = Encode(a, b);
    for (const auto& s : c)
        std::cout << s << std::endl;
    return 0;
}
#包括
#包括
#包括
使用名称空间std;
标准::矢量编码(标准::矢量和文件1,
std::vector和file2){
断言(file1.size()126){
文件3[i]+=(enc%127);
}
否则{
文件3[i]+=(enc+32);
}
}
}
返回文件3;
}
int main(int argc,char*argv[]){
std::向量a{“1”、“2”、“3”};
std::向量b{“6”、“7”、“8”};
用于(const auto&s:a)

std::cout我非常倾向于通过因子分解来简化。在最底层是一个
combine
函数,用于将两个
char
组合成一个:

char combine(char a, char b)
{
    char result = a+b;
    if (result > 126)
        return result % 127;
    return result+32;
}
下一步是迭代两个可能大小不同的字符串中的每个字母。该算法通过“循环”较短的字符串来处理不同长度的字符串

std::string mix(const std::string &first, const std::string &second)
{
    unsigned len1 = first.length();
    unsigned len2 = second.length();
    if (len1 < len2)
        return mix(second, first);
    std::string result;
    // if the strings are of different lengths, first is now the longer
    unsigned j=0;
    for (unsigned i=0; i < len1; ++i, ++j) {
        if (j >= len2)
            j = 0;
        result.push_back(combine(first[i], second[j]));
    }
    return result;
}
请注意,代码当前使用一个
断言
来确保两个
向量的长度相同,但这可能是人为的限制。实际代码应该确保它们的长度相同,或者执行其他操作来处理这种情况。由于不清楚您的函数打算执行什么操作,因此我将其保留让您决定如何处理它,但使用
assert
作为占位符来提醒您确实需要处理它

最后,使用C++11编写一些驱动程序代码:

int main()
{
    std::vector<std::string> english{"one", "two", "three", "four"};
    std::vector<std::string> spanish{"uno", "dos", "tres", "cuatro"}; 

    auto result = Encode(english, spanish);

    std::copy(result.begin(), result.end(), 
              std::ostream_iterator<std::string>(std::cout, " "));
}
intmain()
{
向量英语{“一”、“二”、“三”、“四”};
向量西班牙语{“uno”、“dos”、“tres”、“cuatro”};
自动结果=编码(英语、西班牙语);
复制(result.begin(),result.end(),
std::ostream_迭代器(std::cout,“”);
}

还要注意的是,我使用了
push_back
来附加到字符串的末尾,并对传递的字符串使用
const
声明。

尝试以下三组输入: 1.文件1大于文件2 2.文件2比文件1大 3.文件1的大小等于文件2。 让我们知道错误何时重现,何时未重现。 我认为到这个阶段,你将自己解决这个问题。 如果没有,,
写入文件的内容(尽可能小)file1和file2复制了错误。

首先猜测,
vector
file2中的字符串比
vector
file1中的字符串少。您只检查file1的边界,而不检查file2。使用
std::vector::at
运算符更安全。它将抛出越界异常。如果
file2
的元素较少,则<代码>文件1
文件2[i]
一旦外部循环超过
文件2
的结尾,就会超出范围。知道如何让它循环回到开头吗?我希望它们迭代到文件1的大小,因为如果文件2小于文件1的大小,我希望文件2循环回Begging。使用单独的计数器或使用模运算符。
字符串are immutable
:不,
std::string
是可变的为什么
要断言(file1.size()file2.size())
,并抛出
assert
@quantdev抱歉,是的std::string对于许多语言来说是可变的、固定的。assert用于在调试时检查运行时,如果条件成立,根据示例代码和我的响应中的代码,if file1.size()>file2.size(),当执行行
if(file2[i].size()时嘿,一切看起来都很好,我试着四处看看,但不明白(mix(file1[I],file2[I]);意思是什么?@Cellery72:
mix
是我给混合两个字符串的函数起的名字,所以
mix(file1[I],file2[I]))
只需使用两个
向量中的两个字符串调用该函数即可。我的答案中给出了
mix
的源代码。
std::vector<std::string> Encode(const std::vector<std::string> &file1,
                                const std::vector<std::string> &file2)
{
    std::vector<std::string> file3;

    assert(file1.size() == file2.size());
    for (unsigned int i = 0; i < file1.size(); i++) {
        file3.push_back(mix(file1[i], file2[i]));
    }
    return file3;
}
int main()
{
    std::vector<std::string> english{"one", "two", "three", "four"};
    std::vector<std::string> spanish{"uno", "dos", "tres", "cuatro"}; 

    auto result = Encode(english, spanish);

    std::copy(result.begin(), result.end(), 
              std::ostream_iterator<std::string>(std::cout, " "));
}