[완료] C++ 동적 할당 매개변수 전달에 대해 질문드립니다.

kdnmih의 이미지

안녕하세요
자바만 조금 알고 있는 뉴비입니다.
C++을 해야되서리 조금 보고 있는데 도저히 감을 못잡겠네요.
용어도 익숙치 않아서 검색을 뭐라해야될지도 잘 모르겠고...
해봐도 제가 원하는 답을 찾을 수가 없어서 이렇게 질문을 올렸습니다.

아래와 같이 제가 큐를 만들었습니다.
template
class Queue
{
public:
Queue();

void Enqueue(const T*); // 이부분 구현에서 막히고 있습니다.
T Dequeue(void);

protected:
LinkedList list;
};

아래와 같은 함수가 있습니다.
제가 원하는 목적은 객채를 힙에서 여러개 생성해서 몽땅 넘겨 주고 싶습니다.
template
T* Simulator::Create(void)
{
int qty = rand() % 100;

if ( qty < 9 ) {
qty = 0; // 10 %
} else if ( qty < 29 ) {
qty = 1; // 20 %
} else if ( qty < 79 ) {
qty = 2; // 50 %
} else {
qty = 3; // 20 %
}

T* objects = new T[qty];

return objects;
}

int main()
{
Queue que;
que.Enqueue(simulator.Create());

return 0;
}

제가 잘 모르는 부분이 이부분입니다.

template
void Queue::Enqueue(const T* data) {
//리스트에 값을 하나씩 대입할려면
// data에 몇개의 값이 있는지 알아내야 반복문을 돌릴 수 있는데
// 넘어온 매개 변수에 몇개의 객체가 배열로 할당되어 있는지 어떻게 알수 있을가요?
for( ??) {
list.push_back(?);
}
}

제가 너무 몰라서 질문이 깔끔하지 못하네요ㅠㅠ
읽어주셔서 너무 감사합니다(ㅠㅠ)(__)

Anti-Lock의 이미지

아쉽게도 내장 배열은 요소의 개수를 알수가 없습니다.
1. 배열의 마지막 요소(포인터)를 NULL로 명시하거나(문자열 넘길때 처럼),
2. 요소개수를 넘기도록 인자를 추가하시거나,
3. 리스트나 벡터에 넣어서 'Enqueue'에 넘기시면...

kdnmih의 이미지

개수를 알수가 없는 거였군요...ㅠㅠ 알려주신 3번 방법대로 했습니다. 답변 감사합니다.(^^)(__)

kaeri17의 이미지

그냥 std::vector를 쓰세요. 아니면 매개변수로 개수를 같이 받아야죠..

cleansugar의 이미지

그렇군요

재벌 2세가 재벌이 될 확률과
금메달리스트 2세가 금메달을 딸 확률이 비슷해지도록
자유오픈소스 대안화폐를 씁시다.

아이디의 아이디어 무한도전
http://blog.aaidee.com

귀태닷컴
http://www.gwitae.com

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.