如何开始学习 MinGW C++?

发布于 2024-12-19 06:32:15 字数 1330 浏览 4 评论 0原文

我正在使用代码块学习 C++,我想问您是否可以给我一些好的指导,让我了解 MinGW 和 Visual studio C++ 之间的区别,例如 \n<< 并不总是按照我的预期行事。我是个完全的新手,只读到 Jesse Liberty 所著的一本旧书“Teach myself C++ in 21 days”的第 2 天,程序如下所示:

#include <iostream>

using namespace std;

    int main()
    {
        cout << "Hello there."; endln;
        cout << "Here is 5: " << 5 << endl;
        cout << "The manipulator endln writes a new line to the screen";
        cout << "Here is a very big number:\t" << 70000;
        cout << "Here is the sum of 8 and 5:\t" << 8+5;
        cout << "Here is a fraction:\t\t" << (float) 5/8;
        cout << "And here is a very big number:\t" << (double) 7000*7000;
        cout << "Remember to replace Niklas with you name";
        cout << "Hampus is a C++ programmer!";
        return 0;
    }

MinGW C++ 与 GNU C++ 相同吗?有官方标准吗?任何对新手学习/教学的建议都将得到采纳。我正在关注的书是《21 天自学 C++》,它是这本书的旧版本,但我可以修改第一个练习中的程序来运行,我相信我可以使用这本书,因为可能没有太多更改自从它出版以来,它就针对基本的 C++ 进行了编写(我拥有的这本书的版本可能有 10 年了)。

谢谢你!

更新 在这里得到推荐后,我买了这本书 C++ 入门

I'm learning C++ with codeblocks and I'd like to ask if you can post me some good pointers how I can learn the difference between MinGW and Visual studio C++, for example \n and << don't always behave as I'm expecting. I'm complete newbie, only reached day 2 of an old book "Teach yourself C++ in 21 days" by Jesse Liberty and the program looks like this:

#include <iostream>

using namespace std;

    int main()
    {
        cout << "Hello there."; endln;
        cout << "Here is 5: " << 5 << endl;
        cout << "The manipulator endln writes a new line to the screen";
        cout << "Here is a very big number:\t" << 70000;
        cout << "Here is the sum of 8 and 5:\t" << 8+5;
        cout << "Here is a fraction:\t\t" << (float) 5/8;
        cout << "And here is a very big number:\t" << (double) 7000*7000;
        cout << "Remember to replace Niklas with you name";
        cout << "Hampus is a C++ programmer!";
        return 0;
    }

Is MinGW C++ the same as GNU C++? Is there an official standard? any advice for newbie learning / teaching will be appriciated. The book I'm following is "Teach yourself C++ in 21 days" and it is and old edition of the book but I could modify the programs from the first exercise to run and I believe I can use the book since perhaps not many changes were made to the basic C++ since it was published (the edition of the book I own is maybe 10 years old).

Thank you!

Update
After getting the recommendations here, I've bought the book C++ Primer.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

薆情海 2024-12-26 06:32:15

编译器必须强制执行(或应该强制:P)的唯一事情是用标准编写的内容。标准定义了行为,而不是实现,因此编译器可能有所不同。

首先,code::blocks不是编译器,它是人们通常使用MinGW编译器的环境。另一方面,Visual Studio 是一个恰好带有自己的编译器的环境。

作为一名初级 C++ 程序员和文本编辑器或开发人员,您不必担心编译器之间的差异。你想要使用的环境取决于你,你可以在PSpad中编程(编写代码),然后你可以用数百万种不同的编译器来编译它。

总而言之,编译器必须(或应该)遵守一个标准。编译器以他们喜欢的任何方式实现它,并且可能会添加一些额外的东西(例如可变大小数组静态分配)。请注意,标准对这些扩展一无所知,因此不会定义它们的行为。

然后有某种文本编辑器,您可以在其中编写代码并使用您想要的任何编译器进行编译。

C++ 不是一种容易学习的语言,除非您有较低级别(仍然是高级:))编程语言的经验。发生了很多事情,特别是如果您还没有遇到过指针和引用的话。

我建议你买一本新书,甚至可能包含有关几个月前正式发布的 c++0x / c++11 标准的信息。

另外,不要在 C++ 中使用 (double)x 这种类型转换,因为它实际上是一种类型转换的 C 方式。使用static_cast <双> (x) 在这种情况下。 (还有其他方式来投射。)

The only thing that compiler has to enforce (or SHOULD enforce :P) is stuff that's written in standard. Standard defines behavior, not the implementation, therefore compilers can differ.

First of all, code::blocks is not a compiler, it's an environment in which people usually use MinGW compiler. Visual Studio on the other hand is an environment that just happens to come with it's own compiler.

Differences between compilers should be of no concern for you as a beginning c++ programmer and text editor or dev. environment you want to use is up to you, you can program (write the code) in PSpad and then you can compile it with million different compilers.

To sum it up, there's a standard to which compilers have to (or should) comply. Compilers implement that in whatever way they like and they may add some extra things (like variable size arrays static allocation.) Notice that standard knows nothing about those extensions and therefore doesn't define their behavior.

Then there's a text editor of some kind, in which you write your code and compile it with whatever compiler your heart desires.

C++ is not an easy language to learn unless you have prior experience with lower-level (still high level :)) programming language. There's a lot going on especially if you haven't encountered pointers and references yet.

I suggest you get a new book possibly even containing information on c++0x / c++11 standard which was officially released couple of months ago.

Also, don't use (double)x this kind of typecasting in c++ since it's really a c-way of casting types. Use static_cast < double > (x) in this scenario. (There are other ways to cast too.)

呆头 2024-12-26 06:32:15

只是对“MinGW 和 Visual Studio C++ 之间的区别”的评论:

MinGW 和 Visual C++ 都附带标准 C/C++ 库和 Windows API 库。除此之外,MinGW包还包含许多POSIX C/C++库,例如dirent.h或pthread.h,它们对于在Windows上编写跨平台控制台程序很有用(我在开发模拟程序时这样做了) Windows,在 SEE-GRID 上运行)。

Just a comment to "the difference between MinGW and Visual Studio C++":

Both MinGW and Visual C++ comes with both the standard C/C++ library, and the Windows API libraries. In addition to these, the MinGW package contains lots of POSIX C/C++ libraries, for example dirent.h or pthread.h, which can be useful to write cross-platform console programs on Windows (I did this when I developed simulation programs on Windows, to run on SEE-GRID).

ι不睡觉的鱼゛ 2024-12-26 06:32:15

cout << “你好呀。”;结束;

你有一个错误(我假设)a ;在线的中间。

<<运算符应按照 C++ 标准中的定义工作。这不适合 mingw 和 Visual Studio 或任何其他编译器之间的讨论。

然而 \n 对 Windows 和 Linux 环境有影响。在 Linux 中,每一行都以 \n 字符结束。在 Windows 中,它以 LF(换行)和 CR(回车)结尾。即\r\n。这就是为什么 Linux 文本文件在 Windows 上显示时看起来会非常不同,反之亦然。

无论如何,差异与编译器无关,而是与 行结束字符 在不同的操作系统上。

cout << "Hello there."; endln;

you have an error there of (I assume) a ; in the middle of the line.

The << operator should work as defined in the C++ standard. It's not up for discussion between mingw and visual studio, or any other compiler.

The \n however has implications for Windows and Linux environments. In Linux, every line is ended with a \n character. In Windows it ends with a LF (line feed) and CR (carriage return). ie, \r\n. This is why a linux text file will look very different when displayed on Windows, and vice-versa.

In any case, the difference is nothing to do with the compiler, but with the line-end characters on the different operating systems.

<逆流佳人身旁 2024-12-26 06:32:15

MinGW C++ 与 GNU C++ 相同吗?

是的,MinGW 基本上是 Windows 的 GNU 工具的发行版(MinGW = Windows 的简约 GNU)。它包含一个 GNU C++ 编译器以及其他编程语言的编译器,这些语言是 GCC 的一部分。

有官方标准吗?

C++ 作为一种语言有一个标准。然而,C++ 实现没有“标准”。当然,实现应该尽可能遵循 C++ 标准。 C++ 标准的某些部分声明某些内容是“特定于实现的”——然后由实现决定如何处理该特定情况。

您提到的有关“\n”(换行符)的问题纯粹是特定于平台的。如果你只使用std::cout,那么“\n”就可以工作,但是如果你使用print()或printf(),或者其他一些方法来写入STDOUT,那么你需要小心你的行为。我从你的帖子中看到你使用Windows,其中新行是“\r\n”(回车+换行)。在大多数 POSIX 平台上,“\n”是“行尾”字符,在 Windows 上,它是“\r\n”。在 MacOS X 上,它是“\r”,现在是“\n”。

Is MinGW C++ the same as GNU C++?

Yes, MinGW is basically a distribution of GNU tools for Windows (MinGW = Minimalist GNU for Windows). It contains a GNU C++ compiler among compilers for other programming languages, that are part of the GCC.

Is there an official standard?

There is a standard for C++ as a language. However, there are no "standards" for C++ implementations. Sure, the implementations should follow C++ the standard as close as possible. There parts of the C++ standard that state that something is "implementation specific" - then it is up to the implementation how to deal with that particular case.

The problem you mentioned about "\n" (line-feed character) is purely platform specific. If you only use std::cout, then "\n" will work, but if you use print() or printf(), or some other means to write to the STDOUT, then you need to be careful what you do. I see from your post that you use Windows, where new line is "\r\n" (carriage-return + line-feed). On most POSIX platforms "\n" is the the "end of line" character, on Windows it is the "\r\n". On MacOS X it was "\r", now is "\n".

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文