English 中文(简体)
利用新的经营者
原标题:using new operator

我对新的经营者负有简单的任务。 我需要创建10个果园,然后用cin子投入这些果园。 是否应当如此?

char c = new char[10];
for(int i=0; i < 10; i++)
{
    cin >> char[i] >> endl;
}
问题回答

No. Try char* c = new char[10];

果[一]

无需<代码>endl。 不要忘记<代码>delete []。

char *c = new char[11];  // c should be a pointer.don t forget space for null char.
// do error checking.
cin >> c;                // you can read the entire char array at once.
cout<<c<<endl;           // endl should be used with cout not cin.
delete[]c;               // free the allocated memory.

由于没有人这样做,我建议你使用<代码>std:string。 相反:

std::string w或d;
std::cin >> w或d; // reads everything up to the first whitespace

std::string line;
std::getline(std::cin,line);

使用<条码>的好处是:显示<>/代码>是自动扩展,消除了缓冲外溢。 如果是这样的话,你会处理赤s的缓冲。

void f()
{
  char buffer[10];
  std::cin >> buffer;
  //
}

and someone comes along and enters m或e than 10 characters, then if you are lucky, the whole thing blows up immediately. (If you are unlucky, everything appears to keep w或king until some "funny" err或s manifest much later, probably in seemingly unrelated sections of your code.)

更简单的办法是:

char * c = new char[11];
cin >> c;

为了解释上述答案,“果*”是因为它说,“c”变量将成为点。 你们需要这样做,因为“新”给人留下一些记忆,并给人留下一个点。 使用“双轨”操作器处理利用新运营商分配的记忆,因此,该系统将易于使用。 方括号内的意思是,你将处理阵列点的点子,不应仅仅处理大小(果)的一块记忆,而是应当处理一些阵列。

char c[10];
cin.get( c, 10 );

接近。

char *c = new char[10];
for(int i=0; i < 10; i++)
{
    cin >> c[i];
}

// free the memory here somewhere

更有甚者,如果你真的不需要点子......,就不使用点子。 然后,你不必担心记忆泄露。 (明确提及智能点?)

char c[10];
for(int i=0; i < 10; i++)
{
    cin >> c[i];
}

或者,正如其他人提到的那样,......一度将整整10个果园读。 不同之处在于,this Solutionsspace are received, with cin >> cspace are treatment as delimiters IIRC.





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签