質問

私はC ++プロジェクトでUbuntu環境内のEclipseに取り組んでいます。

私は使用します itoa 機能(Visual Studioで完全に機能する)とコンパイラはそれを文句します itoa 宣言されていません。

含めました <stdio.h>, <stdlib.h>, <iostream> それは役に立ちません。

役に立ちましたか?

解決

www.cplusplus.comは次のように述べています

この関数はANSI-Cで定義されておらず、C ++の一部ではなく、一部のコンパイラによってサポートされています。

したがって、私はあなたがそれを使用しないことを強くお勧めします。ただし、これを非常に簡単に使用することができます stringstream 次のように:

stringstream ss;
ss << myInt;
string myString = ss.str();

他のヒント

itoa() 標準の一部ではないため、使用しないでください。より良い方法があります、すなわち。

C:

int main() {
    char n_str[10];
    int n = 25;

    sprintf(n_str, "%d", n);

    return 0;
}

C ++:

using namespace std;
int main() {
    ostringstream n_str;
    int n = 25;

    n_str << n;

    return 0;
}

ブーストウェイ:

string str = boost::lexical_cast<string>(n);

itoaはコンパイラに依存するため、次の方法をより適切に使用してください。

方法1:C ++ 11を使用している場合は、std :: to_stringを使用してください。それはトリックをします。

方法2:SPRINTFは、C&C ++の両方で機能します。 Ex -ex to_string

#include <bits/stdc++.h>
using namespace std;
int main ()
{
  int i;
  char buffer [100];
  printf ("Enter a number: ");
  scanf ("%d",&i);

  string str = to_string(i);
  strcpy(buffer, str.c_str());

  cout << buffer << endl;
  return 0;
}

注 - -std = c ++ 0xを使用してコンパイルします。

C ++ sprintf:

int main ()
{
int i;
  char buffer [100];
  printf ("Enter a number: ");
  scanf ("%d",&i);
  sprintf(buffer, "%d", i);
  return 0;
}`

Sprintfを使用できます

char temp[5];
temp[0]="h"
temp[1]="e"
temp[2]="l"
temp[3]="l"
temp[5]='\0'
sprintf(temp+4,%d",9)
cout<<temp;

出力は次のとおりです

stdlib.hを含めましたか? (または、C ++を使用しているので、CSTDLIB)

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top