相关疑难解决方法(0)

为什么"使用命名空间std"被认为是不好的做法?

我已经告诉别人,编写using namespace std;代码是错误的,我应该用std::coutstd::cin直接代替.

为什么被using namespace std;认为是不好的做法?是低效还是冒着声明模糊变量(与名称std空间中的函数具有相同名称的变量)的风险?它会影响性能吗?

c++ namespaces using-directives std c++-faq

2486
推荐指数
36
解决办法
78万
查看次数

Pretty-print C++ STL containers

Please take note of the updates at the end of this post.

Update: I have created a public project on GitHub for this library!


I would like to have a single template that once and for all takes care of pretty-printing all STL containers via operator<<. In pseudo code, I'm looking for something like this:

template<container C, class T, String delim = ", ", String open = "[", String close = "]">
std::ostream & operator<<(std::ostream & o, const C<T> …
Run Code Online (Sandbox Code Playgroud)

c++ templates operator-overloading pretty-print c++11

379
推荐指数
5
解决办法
4万
查看次数

我为什么不#include <bits/stdc ++.h>?

我在我的代码中发布了一个问题,其唯一的#include指令如下:

#include <bits/stdc++.h>
Run Code Online (Sandbox Code Playgroud)

我的老师告诉我这样做,但在评论部分,我被告知我不应该这样做.

为什么?

c++ portability c++-faq turbo-c++ implementation-defined-behavior

215
推荐指数
6
解决办法
2万
查看次数

可以提升:algorithm :: join()concat容器的浮点数?

Boost join可用于连接一个字符串容器,可选择用分隔符字符串分隔,如下例所示:boost :: algorithm :: join的一个很好的例子

我的STL技能很弱.我想知道是否有任何方法可以为数字容器(浮点数,双打数,整数)使用相同的函数?看起来应该有一个或两个衬里来适应其他类型.

还有stl的复制功能,这里有一个很好的例子: 如何打印出矢量的内容?

但我不喜欢它如何在每个元素之后添加分隔符字符串.我想使用boost.

c++ string boost stl adapter

16
推荐指数
1
解决办法
6575
查看次数

如何打印出std :: stack的内容并返回其大小?

在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)

c++ stack

8
推荐指数
2
解决办法
2万
查看次数

如何使用 fmt 打印字符向量?

我有一个const std::vector<char>-不是空终止的。我想使用 fmt 库打印它,而不制作矢量的副本。

我本来希望指定精度就足够了,但fmt 文档说:

请注意,即使指定了精度,C 字符串也必须以 null 终止。

嗯,我的不是。我必须复印一份并用 填充它\0,还是我可以做其他事情?

c++ std null-terminated c++14 fmt

5
推荐指数
3
解决办法
2525
查看次数

How do I add a string to a vector (and subsequently display it)?

I am 4 hours-new to C++ and have hit a brick wall with string vectors. When trying to add multiple strings to a string vector, I keep erroring out. I'd like to use push_back.

I would also like to display this string vector, but I'm not sure how (I know how to display non-vectors). Given that I have not been able to add a string to a vector of strings, I did not attempt to display the vector of …

c++ class stdstring stdvector c++11

4
推荐指数
1
解决办法
224
查看次数

C++ 是否可以计算整个向量?

我需要计算一个向量。不仅仅是它的一个元素,而是整个事情。例如 std::cout << vectorName; 类似的东西,希望它是有道理的。有任何想法吗?提前致谢

c++ vector

2
推荐指数
1
解决办法
1万
查看次数

打印出std :: vector <std :: wstring>

我试图改编以打印出std::vector<std::wstring>使用std :: copy 的内容但我不明白该代码是如何工作得很好,并且不能让它编译.代码应该是什么?

我使用了Rob的例子,但它不起作用:

std::vector<std::wstring> keys = ...;
std::copy(keys.begin(), keys.end(), std::ostream_iterator<std::wstring>(std::wcout, " "));
Run Code Online (Sandbox Code Playgroud)

我收到错误:

1>error C2665: 'std::ostream_iterator<_Ty>::ostream_iterator' : none of the 2 overloads could convert all the argument types
1>        with
1>        [
1>            _Ty=std::wstring
1>        ]
1>        C:\Program Files\Microsoft Visual Studio 8\VC\include\iterator(300): could be 'std::ostream_iterator<_Ty>::ostream_iterator(std::basic_ostream<_Elem,_Traits> &,const _Elem *)'
1>        with
1>        [
1>            _Ty=std::wstring,
1>            _Elem=char,
1>            _Traits=std::char_traits<char>
1>        ]
1>        while trying to match the argument list '(std::wostream, const char [2])'
Run Code Online (Sandbox Code Playgroud)

它为什么告诉我_Elem=char …

c++ stl visual-studio-2005

1
推荐指数
1
解决办法
3531
查看次数

重载operator << for set

我有一组整数对,我想打印它,所以我重载了<<运算符的set和pair类:

template<typename T, typename U>
inline ostream& operator<<(ostream& os, pair<T,U> &p){
    os<<"("<<p.first<<","<<p.second<<")";
    return os;
}


template<typename T>
inline ostream& operator<<(ostream& os, set<T> &s){
    os<<"{";
    for(auto it = s.begin() ; it != s.end() ; it++){
        if(it != s.begin())
            os<<",";
        os<<*it;
    }
    os<<"}";
    return os;
}
Run Code Online (Sandbox Code Playgroud)

当我创建一个集合并输出它时

set<pair<int,int>> s;
cout<<s<<endl;
Run Code Online (Sandbox Code Playgroud)

它给出了错误:

cannot bind ‘std::ostream {aka std::basic_ostream<char>}’ lvalue to ‘std::basic_ostream<char>&&’
   os<<*it;
Run Code Online (Sandbox Code Playgroud)

initializing argument 1 of ‘std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&&, const _Tp&) [with _CharT = char; _Traits = std::char_traits<char>; _Tp = std::pair<int, int>]’ …
Run Code Online (Sandbox Code Playgroud)

c++ vector operator-overloading set

1
推荐指数
1
解决办法
89
查看次数

C ++:附加到向量字符串

我正在编写一个“猪拉丁”程序;读取用户的输入(名字和姓氏),使输入变为小写并根据名称中的内容更改名称。如果第一个字母(名字和姓氏)都是元音,我们应该在其末尾添加“ way”。

如果第一个字母是辅音,我们将把第一个字母移到字符串的末尾,并在其末尾添加“ ay”。

尝试在字符串末尾添加文本时,我的代码给我错误。它说不能将字符串转换为字符,我不确定这意味着什么。它还说我不能将输出操作数<<用于字符串,即使我以前使用过它也是如此。

错误出现在“ strcpy”和我输出名称的最终代码中。

37:错误:无法转换'std::vector<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::basic_string<char, std::char_traits<char>, std::allocator<char> > > >''char*'的参数'1''char* strcpy(char*, const char*)'

47:错误:无法转换'std::vector<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::basic_string<char, std::char_traits<char>, std::allocator<char> > > >''char*'的参数'1''char* strcpy(char*, const char*)'

54:错误:不对应的'operator<<''std::cout << first'

我只需要一些帮助来修复错误并查看我哪里出错了。包括完整的代码。

#include <iostream>
#include <vector>
#include <cstring>
#include <string>
using namespace std;
int main()
{
    int q, s;
    char shea[] = "way";
    char gavin_stop_looking_at_ponies[] = "ay";
    vector …
Run Code Online (Sandbox Code Playgroud)

c++ string vector append strcpy

0
推荐指数
1
解决办法
3344
查看次数

如何在c ++中将vector <int>打印到终端

我试图使用"cout"打印整数向量到终端,但我在编译期间收到错误消息:

不匹配'operator <<'(操作数类型是'std :: basic_ostream'和'std :: vector')cout <<"点上的差异:"<< disparityVector << endl;

代码片段如下所示:

vector<int> disparityVector;

for ( int i=0; i<drawPixels.size(); i++)    // Get disparity at each point of the drawn line
  disparityVector.push_back((int)disparityMapOutput.at<int16_t>(pos[i].y, pos[i].x));

cout << "Disparity at points: " << disparityVector << endl;
Run Code Online (Sandbox Code Playgroud)

将值分配给向量时没有错误,只有代码的"cout"部分出错

c++ cout vector

0
推荐指数
1
解决办法
136
查看次数

'<<'运算符的C++错误(对cout的向量内容)

我想cout在以下简单程序中的一个向量的内容:

#include<iostream>
#include<ios>
#include<iomanip>
#include<string>
#include<algorithm>
#include<vector>
using namespace std;

int main()
{
    string name;
    double median;
    int x;
    vector<double> numb, quartile1, quartile2, quartile3, quartile4;

    cout << "Please start entering the numbers" << endl;
    while (cin >> x)
    {
        numb.push_back(x);
    }

    int size = numb.size();
    sort(numb.begin(), numb.end());
    for (int i = 0; i < size; i++)
    {
        double y = numb[(size / 4) - i];
        quartile1.push_back(y);
    }
    cout << quartile1; // Error here
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

每当我尝试编译这个时,我都会收到此错误:

Error …
Run Code Online (Sandbox Code Playgroud)

c++ c++11

-2
推荐指数
1
解决办法
796
查看次数