stl algorithm 질문

auditory의 이미지

short s[size];
double d[size];
 
for (int i=0; i < size; i++)
  s[i] = static_cast< short >(d[i]);

를 stl의 알고리즘 (copy, transform??) 를 써서 한다면
어떻게 깔끔하게 할 수 있을까요?

double->short로의 data loss는 별개로 하고요..

transform에다가 unary function을 정의해서 하면 될것 같은데,
더 간단한 방법이 있을까요?

@ code 블럭 안에 있어도 < > 안에 있는 단어는 이상하게 처리되는군요..

neogeo의 이미지

위의 코드로 충분히 간단해 보입니다만...

굳이 원하신다면..

void out(short x)
{
	std::cout << x << " ";
}
 
...
 
const int size = 10;
short s[size] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
double d[size] = {0.1, 0.2, 0.3, 0.4, 1.5, 2.6, 3.7, 4.8, 5.9, 6.0};
std::copy(d , d + size , s );
std::for_each(s, s + size, out);
std::cout << std::endl;

결과는

0 0 0 0 1 2 3 4 5 6

Neogeo - Future is Now.

Neogeo - Future is Now.

auditory의 이미지

질문은 원래,

std::copy(d , d + size , s );

가 가능한가하는것이었습니다.
d와 s의 타입이 다른 경우에요..

안될것 같아서 미리 질문드린거였는데, 해보니깐 잘 되네요...

gcc 의경우에 -Wall 해도 컴파일 워닝도 없네요.. 좀 의외.
vc++ 의경우에는 template 어쩌고 경고가 무지하게 나네요.

답변 감사합니다.

neogeo의 이미지

기본 type에 대한 경우는 묵시적으로 static cast 가 이미 다 준비가 되어있다고 보시는게 맞을 것 같습니다. ( C++ 의 C 언어 type 을 위한 지원 때문이겠지요. )

뭐 어차피 casting operator 자체도 overloading 되니까 어떤 사용자의 class type 이라도 간단하게 operator 만 ( 혹은 constructor 만 ) 작성해주면 아무 문제없이 잘 되겠지요.

Neogeo - Future is Now.

Neogeo - Future is Now.

댓글 달기

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
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.