저는 C ++ 초보자이며 공백으로 구분 된 정수를 입력해야하고 프로그램이 가능한 모든 정수 배열을 출력해야하는 프로젝트를 진행하고 있습니다. 파이썬에서는을 사용하여 수행 할 수 있다는 [int(item) for item in input().split()]
것을 알고 있지만 C ++에서 동일한 작업을 수행하는 방법을 모르겠습니다. C ++에 내장 된 쉬운 방법을 사용하고 싶습니다. 누구든지 의견을 제시 할 수 있습니까? 어떤 도움을 주시면 감사하겠습니다.
보시다시피 문자열에서 정수 벡터를 생성 한 다음 벡터를 순회합니다.
#include <algorithm>
#include <iostream>
#include <iterator>
#include <sstream>
#include <string>
#include <vector>
int main() {
std::string str;
std::getline(std::cin, str);
std::istringstream iss(str);
std::vector<int> vec;
int temp = 0;
while (iss >> temp) {
vec.push_back(temp);
}
//you now have a vector of integers
std::sort(vec.begin(), vec.end()); //this is a must as `std::permutations()` stops when the container is lexicographically sorted
do {
std::copy(vec.begin(), vec.end(), std::ostream_iterator<int>{std::cout, " "});
std::cout << "\n";
} while (std::next_permutation(vec.begin(), vec.end()));
return 0;
}
가능한 모든 길이의 모든 순열을 출력하는 방법을 알아 보려면
전체 길이보다 적은 자리 수에 대해 STL을 사용하여 C ++에서 순열을 만드는 방법을 참조하세요.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다