Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/127.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/logging/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 复制并反转C+中的字符*+;_C++_String_Algorithm_Pointers_Char - Fatal编程技术网

C++ 复制并反转C+中的字符*+;

C++ 复制并反转C+中的字符*+;,c++,string,algorithm,pointers,char,C++,String,Algorithm,Pointers,Char,我想将反向的char*复制到另一个char*。我在输出的第二行遗漏了一个字母 我做到了: #include <iostream> using namespace std; void cp(char *str2, char *str1){ char *pom1 = str1; while(*pom1){ pom1++; } char* pom2 = str2; while(*pom1 != *str1){ p

我想将反向的
char*
复制到另一个
char*
。我在输出的第二行遗漏了一个字母

我做到了:

#include <iostream>

using namespace std;

void cp(char *str2, char *str1){
    char *pom1 = str1;
    while(*pom1){
        pom1++;
    }
    char* pom2 = str2;
    while(*pom1 != *str1){
         pom1--;
        *pom2 = *pom1;
         pom2++;
    }
    *pom2 = '\0';
}

int main()
{
    char *str1 = "ppC", str2[10] = "Witaj";
    cout << "Napis str2 "<< str2 << endl;
    cp(str2,str1);
    cout << "Napis str2 "<< str2 << endl;
    cp(str2,"CJP");
    cout << "Napis str2 "<< str2 << endl;
    return 0;
}
虽然它应该是:

Napis str2 Witaj
Napis str2 Cpp
Napis str2 PJC

错误就在函数的这个语句中

while(*pom1 != *str1){
一定有

while( pom1 != str1){
考虑到字符串文字具有常量数组的类型。例如,变量str1必须声明为

const char *str1 = "ppC";
void cp( char *str2, const char *str1 );
此外,该函数还应声明为

const char *str1 = "ppC";
void cp( char *str2, const char *str1 );

另外,知道在header
:)中声明了标准算法
std::reverse\u copy

该函数的语句中有错误也是很有用的

while(*pom1 != *str1){
一定有

while( pom1 != str1){
考虑到字符串文字具有常量数组的类型。例如,变量str1必须声明为

const char *str1 = "ppC";
void cp( char *str2, const char *str1 );
此外,该函数还应声明为

const char *str1 = "ppC";
void cp( char *str2, const char *str1 );

另外,知道在标题
:)中声明了标准算法
std::reverse\u copy

也很有用。在这种情况下,只需使用标准库
std::reverse\u copy()

std::reverse_copy( input , input + strlen( input ) , output );

在这种情况下,只需使用标准库,
std::reverse\u copy()

std::reverse_copy( input , input + strlen( input ) , output );
stdlib中有
reverse\u copy

。。。它的用法如下:

template <typename CharT, size_t Ndest>
void cp(CharT (&dest)[Ndest], CharT const *src){
    auto f = src, l = src + std::strlen(src);
    assert(std::distance(f,l) < Ndest);
    *(std::reverse_copy(f, l, dest)) = '\0';
}
模板
无效cp(图表和目的地)[Ndest],图表常数*src){
自动f=src,l=src+std::strlen(src);
断言(std::距离(f,l)
因此,请参见

#include <iostream>
#include <algorithm>
#include <cstring>
#include <cassert>

template <typename CharT, size_t Ndest>
void cp(CharT (&dest)[Ndest], CharT const *src){
    auto f = src, l = src + std::strlen(src);
    assert(std::distance(f,l) < Ndest);
    *(std::reverse_copy(f, l, dest)) = '\0';
}

#include <iostream>

int main()
{
    char str1[]   = "ppC";
    char str2[10] = "Witaj";

    std::cout << "Napis str2 "<< str2 << std::endl;
    cp(str2, str1);
    std::cout << "Napis str2 "<< str2 << std::endl;
    cp(str2,"CJP");
    std::cout << "Napis str2 "<< str2 << std::endl;
    return 0;
}
#包括
#包括
#包括
#包括
模板
无效cp(图表和目的地)[Ndest],图表常数*src){
自动f=src,l=src+std::strlen(src);
断言(std::距离(f,l)reverse\u copy

…它的用法如下:

template <typename CharT, size_t Ndest>
void cp(CharT (&dest)[Ndest], CharT const *src){
    auto f = src, l = src + std::strlen(src);
    assert(std::distance(f,l) < Ndest);
    *(std::reverse_copy(f, l, dest)) = '\0';
}
模板
无效cp(图表和目的地)[Ndest],图表常数*src){
自动f=src,l=src+std::strlen(src);
断言(std::距离(f,l)
因此,请参见

#include <iostream>
#include <algorithm>
#include <cstring>
#include <cassert>

template <typename CharT, size_t Ndest>
void cp(CharT (&dest)[Ndest], CharT const *src){
    auto f = src, l = src + std::strlen(src);
    assert(std::distance(f,l) < Ndest);
    *(std::reverse_copy(f, l, dest)) = '\0';
}

#include <iostream>

int main()
{
    char str1[]   = "ppC";
    char str2[10] = "Witaj";

    std::cout << "Napis str2 "<< str2 << std::endl;
    cp(str2, str1);
    std::cout << "Napis str2 "<< str2 << std::endl;
    cp(str2,"CJP");
    std::cout << "Napis str2 "<< str2 << std::endl;
    return 0;
}
#包括
#包括
#包括
#包括
模板
无效cp(图表和目的地)[Ndest],图表常数*src){
自动f=src,l=src+std::strlen(src);
断言(std::距离(f,l)
int len(const char *p) {
    int c = 0;
    while (*p != '\0')
    {
        c++;
        p++;
    }
    return(c);
}

void cp(char *str2, const char *str1){
if(!(len(str2)<len(str1))){
   const char *pom1 = str1;

    while(*pom1){
        pom1++;
    }
    char* pom2 = str2;
   while( pom1 != str1){
         pom1--;
        *pom2 = *pom1;
         pom2++;
    }
    *pom2 = '\0';
}
}
int len(常量字符*p){
int c=0;
而(*p!='\0')
{
C++;
p++;
}
返回(c);
}
无效cp(字符*str2,常量字符*str1){
如果(!(len(str2)复制粘贴溶液

int len(const char *p) {
    int c = 0;
    while (*p != '\0')
    {
        c++;
        p++;
    }
    return(c);
}

void cp(char *str2, const char *str1){
if(!(len(str2)<len(str1))){
   const char *pom1 = str1;

    while(*pom1){
        pom1++;
    }
    char* pom2 = str2;
   while( pom1 != str1){
         pom1--;
        *pom2 = *pom1;
         pom2++;
    }
    *pom2 = '\0';
}
}
int len(常量字符*p){
int c=0;
而(*p!='\0')
{
C++;
p++;
}
返回(c);
}
无效cp(字符*str2,常量字符*str1){

如果(你的代码是缺少一对夫妇的代码> const <代码> s:不,你缺少了几个代码:STD::String 。你缺少了一个代码> const :不,你缺少了两个 S.@ RIV你错了。C++中的字符串文字有固定数组的类型。<代码> char * A =“AA”“编译”是完全正确的。@ RIV编译器编译这个语句是完全不重要的。它不影响C++标准的字符串字的类型。@ RIV STR1必须具有限定符const,因为它是根据标准的类型。没有任何“向后兼容”。@ MeTASK:不,因为<代码> const char * =“a”;< /C>是正确的方法。@ RIV是错的。C++中的字符串文字有常数数组的类型。<代码> char * a =“aa”。“编译”是完全正确的。@ RIV编译器编译这个语句是完全不重要的。它不影响C++标准的字符串字的类型。@ RIV STR1必须具有限定符const,因为它是根据标准的类型。没有任何“向后兼容”。@TheMask:No,因为
const char*a=“a”
是正确的方法。