벡터를 함수에 전달하는 방법은 무엇입니까?
벡터를 함수에 대한 인수로 보내려고하는데 어떻게 작동하는지 알아낼 수 없습니다. 여러 가지 방법을 시도했지만 모두 다른 오류 메시지를 표시합니다. 이 부분 만 작동하지 않기 때문에 코드의 일부만 포함합니다. (벡터 "random"은 0에서 200 사이의 임의의 값으로 채워지지만 정렬 됨)
코드 업데이트 :
#include <iostream>
#include <ctime>
#include <algorithm>
#include <vector>
using namespace std;
int binarySearch(int first, int last, int search4, vector<int>& random);
int main()
{
vector<int> random(100);
int search4, found;
int first = 0;
int last = 99;
found = binarySearch(first, last, search4, random);
system("pause");
return(0);
}
int binarySearch(int first, int last, int search4, vector<int>& random)
{
do
{
int mid = (first + last) / 2;
if (search4 > random[mid])
first = mid + 1;
else if (search4 < random[mid])
last = mid - 1;
else
return mid;
} while (first <= last);
return -(first + 1);
}
를 vector
참조 또는 포인터로 전달할 것인지 여부에 따라 다릅니다 (값으로 전달하는 옵션을 분명히 바람직하지 않은 것으로 무시합니다).
참고로 :
int binarySearch(int first, int last, int search4, vector<int>& random);
vector<int> random(100);
// ...
found = binarySearch(first, last, search4, random);
포인터로서 :
int binarySearch(int first, int last, int search4, vector<int>* random);
vector<int> random(100);
// ...
found = binarySearch(first, last, search4, &random);
내부 binarySearch
에서 해당 구성원 을 사용 .
하거나 ->
액세스 해야합니다 random
.
현재 코드의 문제
binarySearch
expects avector<int>*
, but you pass in avector<int>
(missing a&
beforerandom
)- You do not dereference the pointer inside
binarySearch
before using it (for example,random[mid]
should be(*random)[mid]
- You are missing
using namespace std;
after the<include>
s - The values you assign to
first
andlast
are wrong (should be 0 and 99 instead ofrandom[0]
andrandom[99]
You'll have to pass the pointer to the vector, not the vector itself. Note the additional '&' here:
found = binarySearch(first, last, search4, &random);
You're passing in a pointer *random
but you're using it like a reference &random
The pointer (what you have) says "This is the address in memory that contains the address of random"
The reference says "This is the address of random"
Anytime you're tempted to pass a collection (or pointer or reference to one) to a function, ask yourself whether you couldn't pass a couple of iterators instead. Chances are that by doing so, you'll make your function more versatile (e.g., make it trivial to work with data in another type of container when/if needed).
In this case, of course, there's not much point since the standard library already has perfectly good binary searching, but when/if you write something that's not already there, being able to use it on different types of containers is often quite handy.
found = binarySearch(first, last, search4, &random);
Notice the &
.
You're using the argument as a reference but actually it's a pointer. Change vector<int>*
to vector<int>&
. And you should really set search4
to something before using it.
If you use random
instead of * random
your code not give any error
참고URL : https://stackoverflow.com/questions/5333113/how-to-pass-a-vector-to-a-function
'Development Tip' 카테고리의 다른 글
Slack 팀 ID와 채널 ID를 찾는 가장 간단한 방법은 무엇입니까? (0) | 2020.10.08 |
---|---|
virtualenv에 이전 버전의 Django를 어떻게 설치하나요? (0) | 2020.10.07 |
Clojure가 클래스를 정의하는 데 하나가 아닌 5 가지 방법이있는 이유는 무엇입니까? (0) | 2020.10.07 |
nodejs-http.request와 함께 response.write를 사용하는 경우 첫 번째 인수는 문자열 또는 버퍼 여야합니다. (0) | 2020.10.07 |
CSS : 고정 높이의 컨테이너 내부에서 div에 대한 스크롤바를 얻는 방법 (0) | 2020.10.07 |