C++ 将指针字符数组划分到其他数组

C++ 将指针字符数组划分到其他数组,c++,C++,当我打印它时,我得到了如下错误17:1733╠╠╠╠╠╠╠╠17:╠╠。 我想不出来。如果您能解决问题并给我一个更好的方法,我将不胜感激?谢谢你的帮助 #include "stdafx.h" #include <iostream> #include <conio.h> #include <cstdlib> #include <iomanip> #include <string> using namespace std; int mai

当我打印它时,我得到了如下错误
17:1733╠╠╠╠╠╠╠╠17:╠╠。
我想不出来。如果您能解决问题并给我一个更好的方法,我将不胜感激?谢谢你的帮助

#include "stdafx.h"
#include <iostream>
#include <conio.h>
#include <cstdlib>
#include <iomanip>
#include <string>
using namespace std;

int main()
{


    char* time = "173324";

    char holdh[3];
    char holdM[3];
    char holds[3];
    holdh[2] = '\0';
    holdM[2] = '\0';
    holds[2] = '\0';

    int t;
    for (t = 0; t < 6;t++)
    {
        if (t < 2)
            holdh[t] = *(time + t);
        else if (2 <= t < 4) {
            t = t - 2;
            holdM[t] = *(time + t);
            t = t + 2;
        }
        else if (4 <= t < 6)
        {
            t = t - 4;
            holds[t] = *(time + t);
            t = t + 4;
        }
    }


    string h(holdh);
    string M(holdM);
    string s(holds);
    string datex = h + ":" + M + ":" + s;
    cout << datex;
    return 0;
}
#包括“stdafx.h”
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
int main()
{
char*time=“173324”;
charholdh[3];
char-holdM[3];
char持有[3];
holdh[2]='\0';
holdM[2]='\0';
持有[2]='\0';
int t;
对于(t=0;t<6;t++)
{
if(t<2)
holdh[t]=*(时间+t);

else if(2表达式
2您的代码是循环中
if()
语句错误使用的教科书示例

在每次迭代中重复所有相同的检查,而实际上您知道迭代必须在哪里停止


假设if()检查表达式中的每个比较,但它不检查。它计算表达式并检查结果是否与非零值或布尔值
true
等效。因此
if(4)不要忘记文字字符串实际上是常量字符数组,因此指针
time
的类型应该是
const char*
。预期的输出是什么?
在代码页437中是0xCC。所以基本上你打印的是一堆0xCC“╠╠╠╠╠╠╠" 这意味着你,可能是因为一个非终止的Cstring@phuclv
std::string
不是c字符串,它们不是null-terminated@user463035818我还没有看过代码,但是std::string确实包含一个以null结尾的字符串,可以通过
data()
c_str()
访问它。一个bug,第二个迭代器是“end”“源字符串的最后一个元素,即它是最后一个元素。”Swift FrayayPee是true,固定。谢谢。欢迎我们。我们可以只指定三个引用每个部分的字符,并使用它们的地址。只是作为另一个“简单”代码。但是你可以很好地说明C++如何可以用C++语言编写整个程序。将C连接到单个输出线:P@Swift-FridayPie变得非常简单::)可爱的故事开始到结束:)“您假设if()检查其表达式中的每个比较”在OPs代码中没有不被检查的比较,它只是OPs得到的它们的优先级wrong@user463035818有语言<代码>如果0<x<2<代码>x意味着大于0小于两个。但是C和因此C++不使用该语法来解决它将“接近金属”的问题。.这不是优先级,而是完全不理解C没有比较。它有返回布尔值的比较运算符。
0
是比较,而
(0@user463035818两个随后的比较运算符调用,是的。这不是一个比较,这将是一个谓词解决方案,而不是一个布尔表达式。语义。C\C++不使用谓词。对不起,我不明白为什么你认为
a
不是一个比较。也许我以后会读一些书,但我不认为我们会在这里会聚
// Create three arrays and initialize all elements to zero
char holdh[3] = {};
char holdM[3] = {};
char holds[3] = {};

holdh[0] = time[0];
holdh[1] = time[1];
holdM[0] = time[2];
holdM[1] = time[3];
holds[0] = time[4];
holds[1] = time[5];
const char* time = "173324";

std::string h(time + 0, time + 2);
std::string M(time + 2, time + 4);
std::string s(time + 4, time + 6);
std::cout << std::string(time + 0, time + 2) << ':'
          << std::string(time + 2, time + 4) << ':'
          << std::string(time + 4, time + 6) << '\n';
std::cout << std::string_view(time + 0, 2) << ':'
          << std::string_view(time + 2, 2) << ':'
          << std::string_view(time + 4, 2) << '\n';
#include <iostream>
#include <cstdlib>
#include <iomanip>
#include <string>

using std::string;
using std::cout;

int main()
{
    const char* time = "173324"; 
    // note, that making it non-const is a not standard compliant

    char hold[3][3] = {};  // zero initialization

    for (int t = 0; t < 6;t++)
    {
        hold[t / 2][t % 2] = time[t];
    }


    string h(hold[0]);
    string M(hold[1]);
    string s(hold[2]);
    string datex = h + ":" + M + ":" + s;
    cout << datex;
    return 0;
}
string hold[3];  
for (int t = 0; t < 6;t++)
{
    hold[t / 2] += time[t];
}
string datex = hold[0] + ":" + hold[1] + ":" + hold[2];
std::string h(time + 0, time + 2);