如何调用向量函数并从函数返回向量

时间:2017-11-29 12:55:36

标签: c++

我正在编写一个程序,将向量的元素移动到k次。我已准备好代码,但是,我在调用和返回类型为vector的函数时遇到问题。请查看我的代码,并向我提供正确的指导,告诉我在哪里放置有问号的评论。

#include <iostream>
#include<vector>
using namespace std;
vector<int> solution(vector<int> &A, int k);//function to shift the vector elements to the right k times

int main()
{
    int no;// number of times to shift the vector

    vector<int> arr;// array to be shifted

    //initializing vectors
    arr.push_back(1);
    arr.push_back(2);
    arr.push_back(3);
    arr.push_back(4);
    cout<<"write the no of times to rotate elements: "<<endl;
    cin>>no;

    solution(arr, no)//?????? dont know how to call the function

    return 0;
}


vector<int> solution(vector<int> &A, int k){

    int times=(k%A.size());// number of times to be shifted 

    int count=0;


    //while loop to shift times number of TIMES
    while(count<=times){

        for(int i=0; i<A.size();i++)// FOR LOOP TO SHIFT EACH ELEMENT TO THE SIDE ONCE
        {
            if(A[i]!=A[(A.size()-1)])
                A[i]=A[i+1];
            else
                A[i]=A[i-(A.size()-1)];
            count++;
        }

        return A; // ??? how do I return the value of the vector to the main program?
    }
}

0 个答案:

没有答案