在c ++中,如何打印出堆栈的内容并返回其大小?
std::stack<int> values;
values.push(1);
values.push(2);
values.push(3);
// How do I print the stack?
Run Code Online (Sandbox Code Playgroud) Visual C++ 2017干净地编译以下内容,调用用户定义的log:
// Source encoding: UTF-8 with BOM ?
#include <algorithm> // std::for_each
#include <iostream>
#include <math.h> // ::(sin, cos, atan, ..., log)
#include <string> // std::string
void log( std::string const& message )
{
std::clog << "-- log entry: " << message << std::endl;
}
auto main()
-> int
{
auto const messages = { "Blah blah...", "Duh!", "Oki doki" };
std::for_each( messages.begin(), messages.end(), log ); // C++03 style.
}
Run Code Online (Sandbox Code Playgroud)
我认为这是一个编译器错误,因为我设计了代码来显示标识符由于与标准库的名称冲突而如何模糊不清.
这是编译器错误吗?
补充信息:MinGW g ++ 7.2会发出几条错误消息.他们并没有完全提供信息,有15条线抱怨std::for_each …
可能重复:
简单字符串连接
昨天,在我写这篇文章时,有人问道
如果我有一个字符串
x='wow'应用addpython中的函数:Run Code Online (Sandbox Code Playgroud)x='wow' x.add(x) 'wowwow'我怎么能用C++做到这一点?
将add(不存在的)修正为__add__(标准方法)这是一个深刻而有趣的问题,涉及微妙的低级细节,高级算法复杂性考虑,甚至线程!,但它的制定时间非常短简洁的方式.
我将原来的问题 作为我自己的问题重新发布 ,因为我没有机会在删除之前提供正确答案,并且我努力恢复原来的问题,以便我可以帮助增加对这些问题的一般理解,失败.
我把原来的标题"select python or C++"改为......
从而缩小了一点问题.
注意:这是一个问题的答案,以便记录其他人可能觉得有用的技术,以便可能意识到其他人更好的解决方案.请随意添加批评或问题作为评论.也可以随意添加其他答案.:)
问题#1:
#include <iostream>
#include <string>
using namespace std;
auto main() -> int
{
wstring username;
wcout << L"Hi, what’s your name? ";
getline( wcin, username );
wcout << "Pleased to meet you, " << username << "!\n";
}
Run Code Online (Sandbox Code Playgroud)
H:\personal\web\blog alf on programming at wordpress\002\code>chcp 65001
Active code page: 65001
H:\personal\web\blog alf on programming at wordpress\002\code>g++ problem.input.cpp -std=c++14
H:\personal\web\blog alf on programming at wordpress\002\code>a
Hi, whatSøren Moskégård
← No … 在¹comp.lang.c++ Usenet组中,我最近断言,基于我认为我所知道的,Windows的16位wchar_t,使用UTF-16编码,有时需要两个这样的值(称为"代理对")单个Unicode代码点,对于表示Unicode无效.
这肯定是不方便的,并且与C和C++标准库(例如字符分类)的假设相冲突,每个代码点都表示为单个值,尽管Unicode联盟的2004年"技术说明12"为使用UTF-16提供了一个很好的例子.用于内部处理,具有令人印象深刻的软件列表.
当然,似乎最初的意图是wchar_t每个代码点有一个值,与C和C++标准库的假设一致.例如,在ununix.org上的网页"ISO C修订1(MSE)"中,关于wchar_t1995年引入C标准的修订,作者坚持认为
"一个字节/一个字符模型的主要优点是,在固定宽度的块中处理数据非常容易.出于这个原因,人们发明了广泛性的概念.宽字符是一种抽象数据类型,其大小足以包含特定平台上支持的最大字符.
但事实证明,C和C++标准似乎并没有谈到支持的最大字符,而只讨论支持的语言环境中最大的扩展字符集:它必须足够大才能代表最大的扩展字符中的每个代码点当没有Unicode语言环境时,设置 - 但不是Unicode.wchar_t
" [
wchar_ttype]是一个整数类型,其值范围可以表示支持的语言环境中指定的最大扩展字符集的所有成员的不同代码.
这几乎与C++标准中的措辞完全相同.并且它似乎意味着使用受限制的一组受支持的语言环境,wchar_t确实可以很小,直到使用UTF-8编码的单个字节(例如,没有标准库字符分类功能在ASCII的A到Z之外工作的噩梦可能性,但是,嘿).可能以下要求比这更广泛:
"甲宽字符是类型的对象的代码值(二进制编码的整数)
wchar_t对应于扩展字符集的成员.
...因为它引用了扩展字符集,但该术语似乎无法在任何地方进一步定义.
至少在Microsoft的C和C++运行时,没有Unicode语言环境:该实现setlocale仅限于每个字符最多2个字节的字符编码:
setlocale:
"可用的区域设置名称,语言,国家/地区代码和代码页的集合包括Windows NLS API支持的所有内容,除了每个字符需要两个以上字节的代码页,例如UTF-7和UTF-8.如果您提供的代码页值为UTF-7或UTF-8,
setlocale则会失败,返回NULL.
所以它似乎与我认为的相反,与我的断言相反,Windows的16位wchar_t正式可以.主要是由于Microsoft巧妙地缺乏对UTF-8语言环境的支持,或者每个字符超过2个字节的任何语言环境.但它真的如此,16位wchar_t好吗?
链接:
¹消息:comp.lang.c ++
² http://unicode.org/notes/tn12/#Software_16
³ http://www.unix.org/version2/whatsnew/login_mse.html
⁴ https://开头MSDN. microsoft.com/en-us/library/x99tb11d.aspx
码:
auto main() -> int
{
int b = 42;
auto lambasta = [&]()
{
using B_type = decltype( b );
return B_type{};
};
(void) lambasta;
}
Run Code Online (Sandbox Code Playgroud)
使用MinGW g ++ 6.3.0进行无诊断编译-std=c++14 -Wall -pedantic-errors.无法使用Visual C++ 2015 update 3进行编译,
foo.cpp(6): error C2065: 'b': undeclared identifier
我有一些使用接口指针的COM代码.代码的原作者实现了返回接口指针的函数,如下所示:
HRESULT Query ( IN BSTR sQuery, OUT IEnumWbemClassObject* &pEnumerator ); // (1)
Run Code Online (Sandbox Code Playgroud)
而不是传统的
HRESULT Query ( IN BSTR sQuery, OUT IEnumWbemClassObject** ppEnumerator ); // (2)
Run Code Online (Sandbox Code Playgroud)
函数(1)被调用如下:
hRes = Query ( sQuery, pEnumerator ); // (3)
Run Code Online (Sandbox Code Playgroud)
这肯定看起来不对,但它工作正常.我不确定我是否只是拿起这一行,因为out参数不是指向输出变量的指针,或者因为这种方法有问题.
使用引用指针而不是指针指向输出参数是否有优势?
我瞪着它并试图在SO上找到类似的问题,但没有找到任何有用的东西.所以,在这里发布我的问题.
考虑这个程序:
#include <iostream>
void foo(const std::string &) {}
int main()
{
foo(false);
}
Run Code Online (Sandbox Code Playgroud)
[Warning] converting 'false' to pointer type for argument 1 of 'std::basic_string::basic_string(const _CharT*, const _Alloc&) [with _CharT = char; _Traits = std::char_traits; _Alloc = std::allocator]' [-Wconversion-null]
为什么C++在没有显式强制转换的情 我原以为会遇到编译错误.由于以下异常显示,程序在运行时异常终止:
terminate called after throwing an instance of 'std::logic_error'
what(): basic_string::_S_construct null not valid
This application has requested the Runtime to terminate it in an unusual way.
Please contact the application's support team for more information.
Run Code Online (Sandbox Code Playgroud)
关于这种隐式转换的标准是什么?
我很确定Visual C++ 2015在这里有一个bug,但我并不完全确定.
码:
// Encoding: UTF-8 with BOM (required by Visual C++).
#include <stdlib.h>
auto main()
-> int
{
auto const s = L""
" is not in the Unicode BMP!";
return s[0] > 256? EXIT_SUCCESS : EXIT_FAILURE;
}
Run Code Online (Sandbox Code Playgroud)
使用g ++的结果:
[H:\scratchpad\simple_text_io] > g++ --version | find "++" g++ (i686-win32-dwarf-rev1, Built by MinGW-W64 project) 6.2.0 [H:\scratchpad\simple_text_io] > g++ compiler_bug_demo.cpp [H:\scratchpad\simple_text_io] > run a Process exit code = 0. [H:\scratchpad\simple_text_io] > _
Visual C++的结果:
[H:\scratchpad\simple_text_io] > cl /nologo- 2>&1 | …
如何使用C++找到解决此问题的算法:给定整数z <= 10 ^ 100,找到包含数字z的Pascal三角形的最小行.
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
Run Code Online (Sandbox Code Playgroud)
例如,如果z = 6 =>结果在第4行.
描述问题的另一种方式:给定整数z <= 10 ^ 100,找到最小整数n:存在整数k,使得C(k,n)= z.
C(k,n)是一次取k而不重复的n个事物的组合