小编Med*_*nic的帖子

比较字符串,c ++

我有个问题:

假设有两个std::strings,我想比较它们,可以选择使用类的compare()功能,string但我也注意到可以使用简单的< > !=运算符(即使我不包括<string>图书馆).compare()如果可以使用简单的运算符进行比较,有人可以解释为什么函数存在吗?

顺便说一句,我使用Code :: Blocks 13.12这里是我的代码示例:

#include <iostream>
#include <string>

using std::cin;
using std::cout;
using std::endl;
using std::string;
using std::getline;

int main()
{
    string temp1, temp2;
    cout << "Enter first word: ";
    getline (cin,temp1);
    cout << "Enter second word: ";
    getline (cin,temp2);
    cout << "First word: " << temp1 << endl << "Second word: " << temp2 << endl;
    if (temp1 > temp2)
    {
        cout << …
Run Code Online (Sandbox Code Playgroud)

c++ string comparison

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

如何处理错误的输入 C?

初学者问题:想象一下这个场景:我要求用户输入一个整数(通过使用 scanf 获取它)但用户输入一个字符,因为程序到达了它的结尾......但我想克服它,并使程序告诉他他提供了无效的输入并给用户另一个输入输入的机会,我该怎么做?

c input scanf

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

c 中的 srand 函数中的“time(null)”之前是否需要“(unsigned int)”?

我看过一些关于用 C 生成随机数的指南:有两件事让我想知道:

  1. 据说除了 stdlib.h 和 time.h 库之外,我还必须包含 math.h 库才能使其工作,为什么?(据我所知,srand 和 rand 函数位于 stdlib 中)?
  2. 在示例中,srand 函数的编写方式如下:

    srand((unsingned int)time(NULL);
    
    Run Code Online (Sandbox Code Playgroud)

我正在使用代码块,并且在没有 unsigned int 和数学库的情况下它也可以正常工作,那么为什么他们将其包含在示例中呢?

谢谢!

c random casting srand

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

C中的指针数学问题

我有一个问题,我今天开始学习指针并且出现了一些奇怪的东西:根据我的指南,通过在创建的指针中添加1,程序将进入内存中的下一个变量.可以看到打印点+ 1的地址,但是当我尝试打印*(点+ 1)的值时,它只打印d的地址?

  int d = 5;
    int e = 12;
    int *point = &d;
    printf("\n\n%u %i\n%u", point, *point, point + 1);
    printf("\n%i", *(point + 1)); 
Run Code Online (Sandbox Code Playgroud)

为什么会这样?顺便说一句我正在使用代码块

c pointers

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

如何从方法返回嵌套类指针?

我有两个类:DatabaseNode为嵌套类,是有可能有一个方法Node,将返回一个Node*

我试图将方法nextNode返回类型设置为Node*但我收到编译错误:'Node' does not name a type

Databse.h

class Database
{
    public:
        Database();
        Database& operator+=(const Client&);
        ~Database();
    private:
        class Node //node as nested class
        {
            public:
            Node(); //ctor
            void setHead(Client*&); //add head node
            Node* nextNode(Node*&); //return new node from the end of he list
            private:
            Client* data; //holds pointer to Client object
            Node* next; //holds pointer to next node in list
        };

        Node *head; //holds the head …
Run Code Online (Sandbox Code Playgroud)

c++ linked-list

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

标签 统计

c ×3

c++ ×2

casting ×1

comparison ×1

input ×1

linked-list ×1

pointers ×1

random ×1

scanf ×1

srand ×1

string ×1