小编Lih*_*ihO的帖子

C指针算术和数组访问

我有一个函数,它接受一个结构数组的指针

typedef struct {
    bool isUsed;
    int count;
} MyStructure;

void Process(MyStructure *timeStamps, int arrayLength){
    for (int i = 0; i < arrayLength; i++){
        MyStructure *myStructure = &(*(timeStamps + i));
        if (myStructure->isUsed == true){
          /*do something*/
        }
    }

}
Run Code Online (Sandbox Code Playgroud)

我访问阵列的方式似乎有些偏差.

&(*(timeStamps + i))
Run Code Online (Sandbox Code Playgroud)

有一个更好的方法吗?

c arrays pointers pointer-arithmetic

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

使用*this来初始化引用

我正在尝试ShadeRec使用其构造函数初始化我的类的实例:

ShadeRec(World& world);
Run Code Online (Sandbox Code Playgroud)

所以我转到它:

ShadeRec sr(*this);
Run Code Online (Sandbox Code Playgroud)

其中"this"是World类的一个实例.

我收到以下错误:

World.cpp: In member function ‘ShadeRec World::hitObjects(const Ray&) const’:
World.cpp:52: error: no matching function for call to ‘ShadeRec::ShadeRec(const World&)’
ShadeRec.h:17: note: candidates are: ShadeRec::ShadeRec(const ShadeRec&)
ShadeRec.h:15: note:                 ShadeRec::ShadeRec(World&)
Run Code Online (Sandbox Code Playgroud)

假设问题只是World实例具有属性const,我该如何摆脱此错误消息?

c++ constructor pointers reference argument-passing

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

在循环中写入文件(std :: ofstream)只写入最后一行

我正在使用此代码提取文本文件的每一行的某些部分:

std::ifstream file( "infile.txt" );
std::string in1, out1;
int blockNumber = 0;

while( getline( file, in1 ) ) 
{   
    int n = 0;
    int i = 0;

    while( i <= blockNumber )
    {   
        n = in1.find_first_of("(", n + 1); 
        i++;
    }   
    out1 = in1.substr( n + 1, ( in1.find_first_of(")", n) - n - 1) );  
    ofstream fmatch ("solo_matches.txt",ios::out);
    fmatch.close();
    fmatch.open("solo_matches.txt");
    fmatch << out1;
    fmatch.close();
} 
Run Code Online (Sandbox Code Playgroud)

但是当我运行代码时,结果并不像我预期的那样.只有最后一个字符串被写入该文件.如果我改用它:

 std::cout << out1 << std::endl;
Run Code Online (Sandbox Code Playgroud)

我得到了我需要的确切输出.我不明白有什么区别.

c++ file-io parsing file ofstream

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

如何在不使用计数器的情况下检查数组的元素是否相同?

假设我有一个数组

 bool string[N]={false};
Run Code Online (Sandbox Code Playgroud)

在执行某些操作之后,数组字符串的所有元素都变为true.我想在if语句中检查这种情况,如下所示: -

伪代码 -

if(all the elements of string are same or equal)
 then do this
Run Code Online (Sandbox Code Playgroud)

我怎么做到这一点?我不应该使用像这样的计数器

for(int i=0;i<N;i++)   //or something else like this 
Run Code Online (Sandbox Code Playgroud)

c arrays comparison

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

fscanf无法正确读取文件〜以HEX格式读取字节的问题

我目前正在尝试做一些我用C++做过几十次的事情,但这是我第一次用C语言做的事情.

我有一个包含3列的巨大文本文件:hexNumber unsignedChar int

adam  38      1
john  39      1
sara  3a      1
frank        3b      0
Christopher        3c      0
kate        3d      0
Run Code Online (Sandbox Code Playgroud)

但是,就像我说文件很大,然后之间的空白因某种原因而变化.我不知道,我没有制作文件.我理解它的方式fscanf是由空格分隔的,所以任何金额都应该没问题吧?

我正在尝试将其读入一个结构数组,这是我的代码:

typedef struct node {
    unsigned char myHex;
    char* myString;
    int myInt;
} node;

void foo(bar* c){

    if( c == NULL )
        return;

    struct node nArr[205] ;

    //read in opcode information
    FILE *fp;
    fp = fopen( "input.txt" , "r" );

    if ( fp == NULL ) {
        fprintf( stderr, "Can't open input file file.txt!\n");
        exit(-1);
    }

    int …
Run Code Online (Sandbox Code Playgroud)

c file-io hex struct scanf

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

如何将char数组中的前15个字符转换为C++中的std :: string?

我知道我可以将字符数组转换为std::string使用:string str(array);

但问题是:可以参与其中并转换为字符串吗?(例如,前15个字符)

顺便说一句,我的数组是在堆栈上定义的.

谢谢.

c++ arrays string

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

如何对包含通过索引相互关联的数据的多个数组进行排序

我有一组包含城市,国家,纬度和纬度的数组.c ++语言.

ifstream file("worldcities.csv");
getline(file, temporay);
//inputs the file into 4 arrays for each catergories
for (i=0;getline(file,(cities[i]),',');i++)
{
getline(file, countries[i], ',');
getline(file, latitude[i], ',') ;
getline(file, longitude[i]);
}
Run Code Online (Sandbox Code Playgroud)

我如何同时对纬度和经度阵列进行排序,以找到列表中所有其他列表的前五个最低或最高,但同时不会丢失那些长期与之相关的城市和国家的元素?

c++ arrays sorting file-io vector

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

`const char*'到`char'

我是编程新手,并试图改进我的基本倒数计时器.我不知道为什么我收到此错误,其他问题在不同情况下,因此不适合我的程序.

//countdown timer using while loops, if else, strings and sleep

#include <iostream>
#include <windows.h>
#include <string>
using namespace std;

int main ()
{
    char progend[5];
    float a; /* a will be floating point */
    cout << "Enter start the the number you want to count down from" << ".\n";

    while (a>-1) { /* the main program is located here */

        cin >> progend[5];

        if (progend[5] = "end") /* if the user inputs end the program ends */
        {
            a …
Run Code Online (Sandbox Code Playgroud)

c++ string comparison pointers countdown

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

为什么没有方法查找的段错误

我正在寻找关于这段代码的澄清.对A :: hello()的调用有效(我期望一个segv).segfault确实通过对成员x的访问,所以看起来单独的方法解决方案实际上并没有取消引用bla?

我编译优化关闭,gcc 4.6.3.为什么不把bla->你好()爆炸?只是想知道'发生了什么事.谢谢.

class A
{

public:
    int x;

    A() { cout << "constructing a" << endl; }

    void hello()
    {
        cout << "hello a"  << endl;
    }

};

int main()
{
    A * bla;
    bla = NULL;
    bla->hello();    // prints "hello a"
    bla->x = 5;      // segfault
}
Run Code Online (Sandbox Code Playgroud)

c++ null pointers segmentation-fault

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

为什么在迭代C中的单链表时,NULL指针检查不起作用?

我正在尝试从堆打印.如果我遇到一个NULL指针我应该打印NULL; 否则,打印它的价值.

样本输出:

1   [2]
2   null
3   null
4   [7, 3]
5   null
6   [7]
Run Code Online (Sandbox Code Playgroud)

但是我的代码因解除引用NULL指针而不断崩溃.

这是我写的测试代码:

void printResult(IntList* intL, int nNode, int nEdge)
{
    int i;
    for (i; i <= 10; i++)
    {
        if (intRest((intL))
        {
            printf("%d", intFirst((intL)[i]));
            intRest((intL)[i]);
        }
        else
            printf(" NULL ");
    }
}

//Here is the definition of functions:
//First
int intFirst(IntList oldL)
{
    return oldL->element;
}

/** rest
 */
IntList intRest(IntList oldL)
{
    return oldL->next;
}
//=================
struct IntListNode
{
    int element;
    IntList next; …
Run Code Online (Sandbox Code Playgroud)

c null pointers loops singly-linked-list

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