所以我有一个目前有两个对象的数组.运行时,显示为"0".
#include "stdafx.h"
#include <process.h>
#include <string>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <iostream>
using namespace std;
int lBlock = rand() % 99 + 10;
int rBlock = lBlock + 1;
string randBlock[15] = { "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F"};
string blocks[2] = {("0x", lBlock, randBlock[rand() % 14], "0"), ("0x", lBlock, randBlock[rand() % 14], "0")};
cout << blocks[1];
Run Code Online (Sandbox Code Playgroud)
任何帮助都会大大贬低!
("0x", lBlock, randBlock[rand() % 14], "0")
Run Code Online (Sandbox Code Playgroud)
此表达式调用comma(,)运算符三次.
在逗号表达式中
E1, E2,表达式E1被计算,其结果被丢弃......并且在表达式的评估E2开始之前完成其副作用.逗号表达式结果的类型,值和值类别正是第二个操作数的类型,值和值类别
E2.
换句话说(a, b) == b,假设:
b == ba不会导致任何可能导致的副作用b != b.因此,上述表达式相当于"0"(除了调用的副作用rand()). std::cout正确显示此值.
看起来你可能意味着这样:
std::string("0x") + std::to_string(lBlock) + randBlock[rand() % 14] + "0"
Run Code Online (Sandbox Code Playgroud)