首页 \ 问答 \ C ++入门的问题(C++ Primer Questions)

C ++入门的问题(C++ Primer Questions)

我目前正在通过Lahoie,Lippman和Moo第五版的C ++ Primer,并且一直在努力解决一些问题。

首先,我只是想确认,在使用任何cctype函数时,我必须确保包含标题,对吗? 因为,最初,我忘了包含它,但它仍然运行。 这真让我困惑。

此外,我正在浏览一个不同的问题(我会去的),并发现另一个问题哈哈! 当使用cctype任何东西时,我是否应该std::/write using std::将其写为std::/write using std::例如,如果我使用std::tolower在每个实例处写入std::tolower /为其写入using语句。 这是有道理的,因为它确实表示它们是“在std命名空间中定义的”,但我没有意识到并一直在写它,没有问题。 我猜size_t类似,对吧?

说到size_t ,我有一个问题。 这是我的代码:

// Exercise Section 3.5.2., Exercise 3.30

#include <iostream>
#include <cstddef>

using std::cout; using std::endl;

int main()
{
    constexpr size_t num_size = 10;
    int num[num_size] = {};

    for (size_t n = 0; n < num_size; ++n) {
        num[n] = n;
        cout << num[n] << endl;
    }
    return 0;
}

因此,代码应该定义一个10个int的数组,并为每个元素赋予与其在数组中的位置相同的值。

它运行正常,但在num[n]=n部分收到错误。 它说Implicit conversion loses integer precision: size_t (aka 'unsigned long') to int

我明白这意味着什么,但我的问题是,本书说“当我们使用变量来为数组下标时,我们通常应该定义该变量来输入size_t ”。 我已经做到了这一点,它给出了这个错误。 它确实运行良好,但它似乎可能导致错误的那种事情。

PS在这段代码中,就像我上面提到的那样,我应该using std::size_t吗?


I am currently going through the 5th edition of C++ Primer by Lahoie, Lippman and Moo and have been struggling with a few things.

Firstly, I just wanted to confirm, when using any of the cctype functions, I have to make sure I include the header, right? Because, initially, I forgot to include it and yet it still ran. That's really confused me.

Also, I was browsing for a different problem (which I'll get to) and found another issue haha! When using anything from cctype, are I supposed to write it as std::/write using std::, e.g. if I use the tolower either write std::tolower at every instance/write a using statement for it. This would make sense as it did say that they are "defined in the std namespace" but I didn't realise and have been writing it without and haven't had an issue. And I'm guessing similar for size_t, right?

Speaking of size_t, I have an issue. This is my code:

// Exercise Section 3.5.2., Exercise 3.30

#include <iostream>
#include <cstddef>

using std::cout; using std::endl;

int main()
{
    constexpr size_t num_size = 10;
    int num[num_size] = {};

    for (size_t n = 0; n < num_size; ++n) {
        num[n] = n;
        cout << num[n] << endl;
    }
    return 0;
}

So the code is supposed define an array of 10 ints and give each element the same value as its position in the array.

It runs correctly, but I am receiving an error at the num[n]=n part. It says Implicit conversion loses integer precision: size_t (aka 'unsigned long') to int.

I understand what this means, but my issue is that the book says "when we use a variable to subscript an array, we normally should define that variable to type size_t". I have done this and it gives this error. It does run fine but it seems like that sort of thing that can lead to errors.

P.S. In this code, like I asked above, should I have using std::size_t?


原文:https://stackoverflow.com/questions/24855637
更新时间:2022-02-08 22:02

最满意答案

首先,看到这个问题 ,这可能会让你的需求没有意义。

在openidenabled.com上对此票的评论似乎也提出了一些解决方法。

其次,我会试着回答你提出的问题(尽管希望没有必要去做你所要求的)。 这听起来像你想尝试在相同的 Apache服务器下运行两个版本的PHP。 一般来说,这可以通过在cgi(或fastcgi)下安装并运行一个或两个PHP版本并调整你的apache配置来为其中一个版本使用一些非标准扩展来完成。 搜索关于运行php4和php5的教程并获得灵感。 理论是一样的。


First of all, see this question, which may make your needs moot.

The comments on this ticket over at openidenabled.com seem to suggest some workarounds as well.

Secondly, I'll try to answer answer the question you asked (even though it's hopefully not necessary to do what you ask). It sounds like you want to try to run two versions of PHP under the same apache server. This can be done, generally speaking, by installing and running one or both of the PHP versions under cgi (or fastcgi), and tweaking your apache configuration to use some non-standard extension for one of the versions. Googling around for tutorials on running php4 and php5 side-by-side for inspiration. The theory is the same.

相关问答

更多
  • 可以在Mac OS X 10.2.x(“Jaguar”)和以上版本上Mac OS X使用二进制安装软件包的PKG格式代替二进制分发版来安装MySQL。请注意该软件包不支持旧版本的Mac OS X(例如,10.1.x)。 安装软件包位于硬盘映象(.dmg)文件中,必须首先双击搜索起中的图标来安装该文件。应当安装图像并显示其内容。 注释:在继续安装前,一定要使用MySQL管理器应用程序(在Mac OS X服务器上)或通过命令行的mysqladmin shutdown关闭所有运行MySQL服务器实例。 要想安装M ...
  • 本来每个人的 ~ 目录下有个 Public 文件夹,这个文件夹里的内容是直接通过 http://localhost/~ 分享出去的。在系统设置 -> 共享中有这个配置项。 另外刚才看了下,Mavericks 里没有了,有个模糊的印象应该是 ML 还是哪个版本开始去掉的。 另外,OS X Server 自带了 PostgreSQL Server,没有装 Server.app 的情况下系统是带了 psql 等命令行工具的。
  • 别把 Mac 想得太复杂了, Mac 是最适合电脑盲的。 dmg 格式的,双击打开,把图标往 应用程序的文件夹里一拖就 OK了 其他格式的,打开后,点下一步就 OK了
  • 在回答之前先向楼上童鞋致以严重的鄙视.. 两者的功能完全一样,只是面向的系统板本不一样.有combo的可以从任何版本的Leopard更新到目标版本,而没有combo的必须从上一版本更新.简单吧~
  • 试过以上,但修改权限的选项不适用于htdocs文件夹, 我的解决方案是: 打开应用文件夹 找到XAMPP文件夹 右键单击,获取信息(如上所述) 在弹出窗口中找到“共享和许可”部分 点击'锁定'挂锁符号 输入管理员密码 将“每个人”权限更改为读取和写入 在获取信息窗口中,选择底部的'cog'图标'下拉选项并选择'Apply to enclosed items',这也将调整所有子文件夹的权限。 重新锁定挂锁符号 关闭“获取信息”窗口。 任务完成后,现在可以根据需要填充htdocs文件夹中的子文件夹来填充您的网站 ...
  • /path/to/other/python setup.py install 在.bashrc为各种系统Pythons(例如python25等)创建别名会很有帮助。 关于从默认系统Python更改python指向的内容,我知道如果你为一个较新版本的别名,这将有效,但它可能会导致旧版本的问题,所以要小心(只需我的$ .02)。 /path/to/other/python setup.py install It can be helpful to make aliases for your various ...
  • 你同时安装了Python 2和Python 3。 Python 2带有macos。 所以程序python可能是python2.7一个链接。 你没有提到你如何安装Python 3.如果你还没有这样做,那么从长远来看,最简单的方法就是使用像自制软件一样的软件包管理器。 这是因为像Pillow这样的图像库需要某些共享库才能正常工作。 Homebrew的食谱将确保在安装枕头时安装这些食谱。 其他有用的软件包如numpy 。 所以,删除你的Python 3版本。 安装自制软件并使用它来安装Python 3和枕头。 Y ...
  • 首先,看到这个问题 ,这可能会让你的需求没有意义。 在openidenabled.com上对此票的评论似乎也提出了一些解决方法。 其次,我会试着回答你提出的问题(尽管希望没有必要去做你所要求的)。 这听起来像你想尝试在相同的 Apache服务器下运行两个版本的PHP。 一般来说,这可以通过在cgi(或fastcgi)下安装并运行一个或两个PHP版本并调整你的apache配置来为其中一个版本使用一些非标准扩展来完成。 搜索关于运行php4和php5的教程并获得灵感。 理论是一样的。 First of all, ...
  • ./configure命令是从源代码编译PHP。 这意味着,您将使用自定义版本替换OSX附带的php:请按照此处的文档: http : //php.net/manual/en/install.macosx.compile.php 您应该使用XAMPP或MAMP,而不是尝试自己编译PHP。 它们随附了您需要的一切,并且易于安装和配置。 I solved by changing system PHP to installing php. brew tap homebrew/homebrew-php brew u ...
  • 错误的phpunit版本 $ pear list-all 显示默认通道中的包,即“pear.php.net”,除非您另外配置它。 你确实从pear.phpunit.de安装了phpunit,这是一个不同的频道。 PHPUnit以前(版本1)托管在pear.php.net上,但移动到自己的频道。 运行 $ pear list-all -c pear.phpunit.de 显示phpunit 3.5.13 。 $ pear install phpunit/phpunit 实际上是一样的 $ pear in ...

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)