帮助混合C ++的向量 [英] Help with vectors mixing C++

查看:261
本文介绍了帮助混合C ++的向量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include<iostream>
#include<vector>

using namespace std;

vector<int> mix(vector<int> v1, vector<int> v2)
{
    vector<int> newVec{v1.at(0), v2.at(0)};
        if(v2.size()+2>1){
            vector<int> restV1(v1.begin() + 1, v1.end());
            vector<int> restV2(v2.begin() + 1, v2.end());
                vector<int> call_vec = mix(restV1, restV2);
                newVec.reserve(newVec.size());

                 vector<int> newVeca=newVec;
                 for(size_t i(0);i<newVeca.size();i++){
                    cout<< newVeca[i]<< "|";
                 }
             }
        return newVec;
}

int main()
{
    vector<int> v1{1,2,3,4};
    vector<int> v2{5,6,7,8};

    mix(v1,v2);

    return 0;
}





我的尝试:



嘿,每个人!!!

我有这个功课,我们需要在没有循环的情况下进行操作,我很难理解,这里拖东西

1-为什么new-Vec的顺序不正确(来自递归或我在代码中做错了什么)

2-我可以获得最后的两个整数新的整数

每次我都希望增加if中的大小(if(v2.size()+ 2> 1))我得到这个错误:

(终止抛出'std :: out_of_range'实例后调用

what():vector :: _ M_range_check:__n(为0)> = this-> size()(为0) )







编写一个函数,将两个int值向量作为参数并返回一个向量int值作为返回值。返回的向量应始终包含第一个参数的值和第二个参数的值(从第一个参数的第一个值开始)。结果向量中值的相对顺序应与输入向量中的相对顺序相同。如果两个输入向量的长度不同,则较长向量的其余部分不变



参数:Ergebnis:

{1,2 ,3} {4,5,6} {1,4,2,5,3,6}

{1,2,3,4,5,6} {7,8,9 } {1,7,2,8,3,9,4,5,6}



What I have tried:

Hey every one !!!
I have this homework, that we need to mack it without loop and i have a trouble understanding, the tow thing here
1- why the new-Vec is not in the right order (is that from the recursion or somthing i done wrong in my code )
2- i coudnt get the last tow integer in add the new integer
everytime i'm treing to increse the size in if ( if(v2.size()+2>1))i get this error:
(terminate called after throwing an instance of 'std::out_of_range'
what(): vector::_M_range_check: __n (which is 0) >= this->size() (which is 0))



Write a function that gets two vectors of int values as parameters and returns a vector of int values as a return value. The returned vector should always contain alternately a value from the first parameter and a value from the second parameter (starting with the first value from the first parameter). The relative order of the values should be the same in the result vector as in the input vectors. If the two input vectors are of different lengths, the remainder of the longer vector is unchanged

Parameter: Ergebnis:
{1,2,3} {4,5,6} {1,4,2,5,3,6}
{1,2,3,4,5,6} {7,8,9} {1,7,2,8,3,9,4,5,6}

推荐答案

这个
if(v2.size()+2>1)

始终为true。所以这是一个问题。你没有使用call_vec?不是结果吗?



做一些输出并使用调试器。



如果有可能使用使用6运算符通过引用调用:

always is true. So it is a problem. You arent using the call_vec? Isnt that the result?

Make some output and use the debugger.

if it is possible use call by reference with the 6-operator:

vector<int> mix(vector<int> &v1, vector<int> &v2)

这个aovids在函数调用中创建新实例。 (使用step into调试器命令来证明它9

This aovids creating new instances in the function call. (Use "step into" debugger commands to prove it9


谢谢

完成它





thanks
done it


#include<iostream>
#include<vector>

using namespace std;

vector<int> mix(vector<int> &v1, vector<int> &v2)
{            vector<int> newVec{v1.at(0), v2.at(0)};

        if(v1.size()>1){
            vector<int> restV1(v1.begin() + 1, v1.end());
            vector<int> restV2(v2.begin() + 1, v2.end());
                vector<int> call_vec = mix(restV1, restV2);

        newVec.insert(newVec.end(), call_vec.begin(), call_vec.end());
}

 return newVec;


    }




int main(){

    vector<int> v1{1,2,3,4};
    vector<int> v2{5,6,7,8};

    vector<int> v =  mix(v1,v2);


                 for(size_t i(0);i<v.size();i++){
                    cout<< v[i]<< "|";

        }
}


这篇关于帮助混合C ++的向量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆