相关疑难解决方法(0)

逐字逐句输入字符串

我刚开始学习C++.我只是玩弄它并遇到一个问题,包括逐字逐句输入字符串,每个单词用空格分隔.我的意思是,假设我有

   name  place animal 
Run Code Online (Sandbox Code Playgroud)

作为输入.我想读第一个字,对它做一些操作.然后读取第二个单词,对其进行一些操作,然后读取下一个单词,依此类推.

我试着用这样的getline存储整个字符串

    #include<iostream>
    using namespace std;
    int main()
    {
     string t;
     getline(cin,t);
     cout << t; //just to confirm the input is read correctly
    }
Run Code Online (Sandbox Code Playgroud)

但是,我如何对每个单词执行操作并继续下一个单词?

此外,在谷歌上搜索C++时,我在许多地方看到,而不是使用"使用命名空间std",人们更喜欢用"std ::"编写所有内容.为什么?我认为他们做同样的事情.那么为什么要一次又一次地写它呢?

c++

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

C++标准库:如何为cout,cerr,cin和endl编写包装器?

我不喜欢using namespace std,但我也厌倦了键入std::在每一条战线cout,cin,cerrendl.所以,我想给他们这样简短的新名字:

// STLWrapper.h

#include <iostream>
#include <string>

extern std::ostream& Cout;
extern std::ostream& Cerr;
extern std::istream& Cin;
extern std::string&  Endl;

// STLWrapper.cpp

#include "STLWrapper.h"

std::ostream& Cout = std::cout;
std::ostream& Cerr = std::cerr;
std::istream& Cerr = std::cin;
std::string _EndlStr("\n");
std::string& Endl = _EndlStr;
Run Code Online (Sandbox Code Playgroud)

这有效.但是,上面有什么问题我错过了吗?有没有更好的方法来实现同样的目标?

c++ iostream

16
推荐指数
2
解决办法
5937
查看次数

命名空间"std"没有成员"sort"

尝试对整数数组进行排序,经过一些谷歌搜索后,遇到了std::sort这个错误,遇到了解决方案:namespace "std" has no member "sort".

只是为了消除我没有使用std命名空间的任何疑虑,这里是我的标题:

#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
using namespace std;
Run Code Online (Sandbox Code Playgroud)

c++ sorting std

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

使用"std"的标准惯例

完全重复: 您更喜欢C++中的显式名称空间还是"使用"?

以下哪一项是使用任何命名空间的首选约定?

using namespace std;
Run Code Online (Sandbox Code Playgroud)

要么

using std::cin;
using std::cout;
Run Code Online (Sandbox Code Playgroud)

要么

在代码中需要时调用函数?

std::cout<<"Hello World!"<<std::endl;
Run Code Online (Sandbox Code Playgroud)

c++ using

11
推荐指数
3
解决办法
1128
查看次数

如何解决C++命名空间和全局函数之间的名称冲突?

如果我在log某处定义名称空间并使其在全局范围内可访问,则会与double log(double)标准cmath标题冲突.实际上,大多数编译器似乎也同意它 - 大多数版本的SunCC,MSVC,GCC - 但GCC 4.1.2没有.

不幸的是,似乎没有办法解决歧义,因为using声明对于命名空间标识符是不合法的.你知道我可以log::Log在全局命名空间中编写任何方式cmath吗?

谢谢.

编辑:有人会知道C++ 03标准对此有何看法?我原以为范围运算符足以消除log下面代码示例中的使用歧义.

#include <cmath>

namespace foo
{

namespace log
{

struct Log { };

} // namespace log

} // namespace foo


using namespace foo;

int main()
{
    log::Log x;

    return 0;
}

// g++ (GCC) 4.1.2 20070115 (SUSE Linux)

// log.cpp: In function `int main()':
// log.cpp:20: error: reference to `log' is ambiguous
// /usr/include/bits/mathcalls.h:110: …
Run Code Online (Sandbox Code Playgroud)

c++ namespaces name-conflict

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

包括"vector.h"或"vector"会导致警告或错误

如果我#include <vector.h>输入我的源文件,我会收到此警告:

make -f Makefile CFG=Debug 
g++ -c    -g -o "Debug/mynn.o"  "mynn.cpp"
In file included from C:/MinGW/bin/../lib/gcc/mingw32/3.4.5/../../../../include/c++/3.4.5/backward/vector.h:59,
                 from mynn.h:7,
                 from mynn.cpp:1:
**C:/MinGW/bin/../lib/gcc/mingw32/3.4.5/../../../../include/c++/3.4.5/backward/backward_warning.h:32:2: warning: #warning This file includes at least one deprecated or antiquated header. Please consider using one of the 32 headers found in section 17.4.1.2 of the C++ standard. Examples include substituting the <X> header for the <X.h> header for C++ includes, or <iostream> instead of the deprecated header <iostream.h>. To disable this warning use -Wno-deprecated.**
g++ …
Run Code Online (Sandbox Code Playgroud)

c++

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

如何在以arg形式传递给某些函数的数组中使用结构化绑定?

我正在尝试将给函数的2个整数组成的数组分解为 x, y

int init[2]用作参数时不起作用。但是当我将其更改为int (&init)[2]

vector<vector<State>> Search(vector<vector<State>> board,
                             int init[2], int goal[2]) {
  auto [x, y] = init;
}
Run Code Online (Sandbox Code Playgroud)

(&init)是什么意思?为什么使用时不起作用int init[2]

c++ c++17

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

C++命名空间 - "使用"还是明确声明?

可能重复:
为什么'using namespace std;' 在C++中被认为是一种不好的做法?
使用std命名空间

这只是一个偏好问题吗?或者是否有合理的理由

using namespace std;
#include <string>
myString string;
Run Code Online (Sandbox Code Playgroud)

要么

#include <string>
myString std::string;
Run Code Online (Sandbox Code Playgroud)

我想每次显式声明命名空间,而拖动键入,避免任何名称冲突的可能性(或编译器是否会警告歧义?)

问题:这种或那种方式存在令人信服的争论吗?

c++ coding-style namespaces

6
推荐指数
0
解决办法
482
查看次数

在c ++中没有匹配运算符+错误

这是我一直在研究的Rational类:

rational.h

#include<iostream>

using namespace std;

#ifndef RATIONAL_H
#define RATIONAL_H

class Rational
{
  int numerator,denominator;
  public:
  // the various constructors
  Rational();
  Rational(int);
  Rational(int,int);

  //member functions
  int get_numerator()const{return numerator;}
  int get_denominator()const{return denominator;}

  // overloaded operators
  // relational operators
  bool operator==(const Rational&)const;
  bool operator<(const Rational&)const;
  bool operator<=(const Rational&)const;
  bool operator>(const Rational&)const;
  bool operator>=(const Rational&)const;

  //arithmetic operators
  Rational operator+(const Rational&);
  Rational operator-(const Rational&);
  Rational operator*(const Rational&);
  Rational operator/(const Rational&);

  //output operator
  friend ostream& operator<<(ostream&, const Rational&);
};
#endif //RATIONAL_H
Run Code Online (Sandbox Code Playgroud)

rational.cpp

#include "rational.h"

// …
Run Code Online (Sandbox Code Playgroud)

c++ operator-overloading

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

运算符重载c ++ /在哪里放我的代码?

今天我在我的一个课程中重载了<<运算符:

#ifndef TERMINALLOG_HH
#define TERMINALLOG_HH

using namespace std;

class Terminallog {
public:

    Terminallog();
    Terminallog(int);
    virtual ~Terminallog();

    template <class T>
    Terminallog &operator<<(const T &v);

private:

};

#endif
Run Code Online (Sandbox Code Playgroud)

如您所见,我在头文件中定义了重载运算符,然后我继续在我的.cc文件中实现它:

//stripped code

template <class T>
Terminallog &Terminallog::operator<<(const T &v) {
    cout << endl;
    this->indent();
    cout << v;
    return *this;
}

//stripped code
Run Code Online (Sandbox Code Playgroud)

然后我使用我的新类创建了一个main.cpp文件:

#include "inc/terminallog.hh"

int main() {
    Terminallog clog(3);
    clog << "bla";
    clog << "bla";
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

我继续前进:

g++ src/terminallog.cc inc/terminallog.hh testmain.cpp -o test -Wall -Werror 
/tmp/cckCmxai.o: In function `main':
testmain.cpp:(.text+0x1ca): …
Run Code Online (Sandbox Code Playgroud)

c++ operator-overloading

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