小编Lig*_*ica的帖子

哪种编程语言使用它以及它是如何工作的

我在互联网上找到了这个循环,从那以后我一直试图找出使用它的语言,以便找出它是如何工作的并将其转换为C++

for m from 0 by 1 to n do 
 A[m] ? 1/(m+1)
 for j from m by -1 to 1 do
  A[j-1] ? j×(A[j-1] - A[j])
return A[0] (which is B_n)
Run Code Online (Sandbox Code Playgroud)

编辑:我只是很好奇,因为我还在学习编程和东西

c++ language-agnostic for-loop

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

类中的setter不会设置变量

我目前正在尝试用C++制作游戏.在我的代码中,我试图嵌套我的变量,以便我的主要没有很多包含.我现在的问题是我班上变量的值没有变化.单步执行代码会显示它设置值,但它不起作用.有谁知道发生了什么?先感谢您.

这是我到目前为止:

Location.h

#ifndef LOCATION_H
#define LOCATION_H

#include <string>

class Location
{
public:
    Location(void);
    Location(std::string name);
    ~Location(void);

    std::string GetName();
    void SetName(std::string value);

private:
    std::string m_Name
};

#endif
Run Code Online (Sandbox Code Playgroud)

Location.cpp

#include "Location.h"

Location::Location(void): m_Name("") {}

Location::Location(std::string name): m_Name(name) {}

Location::~Location(void)
{
}

std::string Location::GetName()
{return m_Name;}

void Location::SetName(std::string value){m_Name = value;}
Run Code Online (Sandbox Code Playgroud)

PlayerStats.h

#ifndef PLAYERSTATS_H
#define PLAYERSTATS_H

#include "Location.h"

class PlayerStats
{
public:
    PlayerStats(void);
    ~PlayerStats(void);

    Location GetLocation();
    void SetLocation(Location location);

private:
    Location m_Location;
};

#endif
Run Code Online (Sandbox Code Playgroud)

PlayerStats.cpp

#include "PlayerStats.h"

PlayerStats::PlayerStats(void): m_Location(Location()) {}

PlayerStats::~PlayerStats(void)
{ …
Run Code Online (Sandbox Code Playgroud)

c++ getter-setter

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

如何使函数适用于字符串和wstring?

如果我想要一个像下面这样的功能basic_string<char>,basic_string<wchar>我该怎么办?请注意,它必须wchar_t在无序映射中进行更改.

该函数还需要接受wchar_t数组以支持将参数作为文字传递.

bool isUnique(wstring s)
{
    if (s.size() <= 1)
    {
        return true;
    }
    unordered_map<wchar_t, bool> charSeen;
    for (int i = 0; i < s.size(); i++)
    {
        if (!charSeen[s[i]])
        {
            charSeen[s[i]] = true;
        }
        else
        {
            return false;
        }
    }
    return true;
}
int main(int argc, char *argv[]) {
    cout << isUnique(L"???");
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

c++ string unicode wstring

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

使用dirent.h在C中实现ls程序

我正在尝试使用dirent库实现ls程序.看来我的DIR*mydir是<unspecified type>我用gdb调试的时候,它告诉我,好像我没有正确包含dirent.h头,但我相信我正确地包含了所有必需的头文件.

#include <stdio.h>
#include <stdlib.h>
#include <dirent.h>
#include <sys/types.h> 
#include <sys/stat.h>

//Specifies whether or not to print hidden files
#define PRINT_HIDDEN 0

void readDirectory(char *dirString[]);

int main(int argc,char* argv[]) 
{
if(argc!=2)
{
printf("Usage: ./ls <directory>\n");
}

readDirectory(argv);    

return 0;
}

void readDirectory(char *dirString[])    
{
DIR *mydir; 
struct dirent *entry;
//Opening the directory and checking if valid
mydir = opendir(dirString[1]);  

if(mydir==NULL){
fprintf(stderr,"ls: cannot access %s: No such file or directory\n",
  dirString);
exit(EXIT_FAILURE);
}   
//Printing directories/files in specified directory …
Run Code Online (Sandbox Code Playgroud)

c ls dirent.h

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

空字符串和'\ 0'字符之间有什么区别?(从指针和数组的角度来看)

作为这个问题的标题?有什么不同?

如果我写:

char *cp = "a";
cout << (cp == "a") << endl;
Run Code Online (Sandbox Code Playgroud)

要么:

string str = "a";
cout << (str == "a") << endl;
Run Code Online (Sandbox Code Playgroud)

它们是相同的,都归于真.但如果我写:

char *cp = "";
cout << (cp == "\0") << endl;
Run Code Online (Sandbox Code Playgroud)

它返回false.但:

string str = "";
cout << (str == "\0") << endl;
Run Code Online (Sandbox Code Playgroud)

它返回true.

我认为它们应该从指针和数组的角度来看是一样的.但事实证明它们是不同的.他们之间有什么微妙的区别?我应该如何编写一个char数组来表示一个空字符串?


好吧,上面的内容可能不清楚,就像有人说它可能是"编译器优化".

我真正想要的是这个:

char *cp = "abcd";
Run Code Online (Sandbox Code Playgroud)

可以给我一个像这样的数组:['a','b','c','d','\ 0'].我想知道如何使用类似的语法来获得这样的数组:['\ 0'].因为我试过:

char *cp = "";
Run Code Online (Sandbox Code Playgroud)

而且似乎不是正确的代码.我认为它可以给我我想要的东西.但事实并非如此.

对不起,线路上方的模棱两可.我是新手,我不是那可能是编译器优化.

c++ arrays pointers char

0
推荐指数
2
解决办法
1716
查看次数

混淆是指针和虚函数的内存分配

我读到了虚拟功能,但我无法清除这个概念.在下面提到的例子中.我们创建一个基指针并首先分配基础对象,调用函数是基类,然后分配派生对象并调用其函数.既然我们已经提到过要分配哪些对象,那么编译时编译器不知道要调用哪个对象函数?我不明白为什么这个决定会延迟到运行时间.我在这里遗漏了什么.

#include <iostream>
using std::cout;
using std::endl;

// Virtual function selection
class Base
{
public:
   virtual void print() const
   { 
      cout << "Inside Base" << endl; 
   }
};

class Derived : public Base
{
public:
   // virtual as well
   void print() const
   { 
      cout << "Inside Derived" << endl; 
   }
};

int main()
{
   Base b;
   Derived f;

   Base* pb = &b;  // points at a Base object
   pb->print();    // call Base::print()

   pb = &f;        // points at Derived object …
Run Code Online (Sandbox Code Playgroud)

c++ virtual-functions

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

限制std :: string变量的最大长度

std::string src;
cout<<"maximum length of string : "<<src.max_size();
Run Code Online (Sandbox Code Playgroud)

我得到的输出是:

maximum length of string : 1073741820
Run Code Online (Sandbox Code Playgroud)

我需要创建一个大型字符串数组,如字符串输入[100000],但当我使用超过80,000的数组索引时,我得到运行时错误.我的字符串变量的长度较小,平均长度为15个字符.所以我需要限制字符串变量的长度.

以下是我的问题:

  1. 决定字符串数组的bigt索引的因素是什么?

  2. 如何减少字符串变量的max_size()?

c++ arrays string

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

我可以写入线程中的变量并在c ++中的另一个线程中读取它

我知道我可以使用信号量或互斥量和...,但我想知道如果我在一个线程中写入变量并在另一个线程中读取它是否有任何问题?像这样的东西:

#include <iostream>
#include <thread>

volatile int value=0;

int main()
{
     auto workerThread = new std::thread([&]()
        {
           for(int i=0;i<10000;++i)
           {
                value=i;
                std::this_thread::sleep_for(std::chrono::milliseconds(i*100));
            }
        }
     );

     while(true)
     {
            std::cout<<value<<std::endl;
            if(value >1000) break;
            std::this_thread::sleep_for(std::chrono::milliseconds(10));
     }
}
Run Code Online (Sandbox Code Playgroud)

它是一种安全的应用而且没有气味吗?

没有任何复杂性,我可以改进吗?

c++ multithreading

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

访问仅在父级的cpp文件中的类

我想调用一个只在父源类文件的文件中的类.实际上我在4个文件中有3个类:

parent.h 
parent.cpp 
child.h
child.cpp
Run Code Online (Sandbox Code Playgroud)

"主要"类是ChildParent.该类Otherclass位于类的定义上方的parent.h文件中Parent.

如何才能访问Otherclass仅在child.cpp文件的parent.cpp文件中的类?

(不幸的是,我不允许在父文件中进行大的更改.)

parent.cpp:

using ParentNamespace::Parent;


namespace other
{
    class Otherclass
    {
    public:
        Otherclass()
        {
            // do something
        }

        ~Otherclass()
        {
        }
    };
}


Parent::Parent()...
...
// here the Parent class continues normal
Run Code Online (Sandbox Code Playgroud)

child.cpp:

#include "parent.h"

Child::Child() :
ParentNamespace::Parent()
...
...
...
    // here I want to use Otherclass
Run Code Online (Sandbox Code Playgroud)

c++

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

c ++赋值运算符异常安全

非常直截了当的问题:

要求赋值运算符不能抛出异常的原因是什么?

同时,构造函数可以抛出?


如果你不能抛出异常,如果没有足够的内存用于缓冲区分配,如何处理众所周知的"自定义"字符串示例?

如果你只是分配更少或保持旧状态,但不抛出异常,一切都会"看起来"平滑,但会出现严重(隐藏)错误.

c++ exception assignment-operator c++11

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