PHP에서 다시 작성해야하는 C++의 코드가 있습니다. PHP 프로그래머이고 C++을 잘 모르는 동안 이상한 일이 일어납니다. 코드의C++ unordered_set insert insert PHP
#include <iostream>
#include <unordered_set>
using namespace std;
int func(int choice) {
unordered_set<int> hset;
for (int counter = 0; counter < choice; counter++) {
hset.insert(counter);
if (counter % 2 == 0) {
hset.insert(counter);
hset.insert(counter + 1);
}
}
return hset.size();
}
int main(int argc, char** argv) {
cout << func(561);
return 0;
}
출력은 다음과 같습니다 : 이 코드입니다 562
;
는이 같은 PHP 코드를 작성했습니다 :
는function func($choice) {
$hset = [];
for ($counter = 0; $counter < $choice; $counter++) {
array_push($hset, $counter);
if ($counter % 2 == 0) {
array_push($hset, $counter);
array_push($hset, $counter + 1);
}
}
return count($hset);
}
echo func(561);
이 1123
를 반환합니다. 도와주세요.
를 사용할 수 있을까? 그 목적이 무엇인지 아십니까? 내가 물어 보는 이유는 집합에 중복 된 요소들을 넣기 때문이며'unordered_set'는 각 고유 한 요소의 복사본을 1 개만 유지하기 때문입니다. –
@BenjaminLindley 이것이 문제입니다. OP (Cognise)는 C++을 잘 모릅니다. 그러나 원래 코드의 특정 부분은 의미가 없습니다. –
@MrLister : 네, 궁금한 점은 'unordered_set'이 여기에 일자리를 얻는 데 적합한 컨테이너라고 생각하게 만드는 것입니다. 일반적으로 C++를 배울 때 처음 배울 컨테이너 중 하나가 아니며 대부분의 사람들이 배울 수있는 첫 번째 컨테이너 ('std :: vector')는 afaict이며, 작업에 적합한 컨테이너입니다. –