C++ 数字向后的指针

C++ 数字向后的指针,c++,pointers,C++,Pointers,我需要接收一个介于(0-9)之间的未定数目的整数。使用这些数字,向前和向后打印,然后删除角落处的数字 例如: 3 5 1 9 4 6 2 4 4 2 6 4 9 1 5 3 5 1 9 4 6 2 4 4 2 6 4 9 1 5 1 9 4 6 2 4 4 2 6 4 9 1 9 4 6 2 4 4 2 6 4 9 4 6 2 4 4 2 6 4 6 2 4 4 2 6 2 4 4 2

我需要接收一个介于(0-9)之间的未定数目的整数。使用这些数字,向前和向后打印,然后删除角落处的数字

例如:

3 5 1 9 4 6 2 4 4 2 6 4 9 1 5 3 5 1 9 4 6 2 4 4 2 6 4 9 1 5 1 9 4 6 2 4 4 2 6 4 9 1 9 4 6 2 4 4 2 6 4 9 4 6 2 4 4 2 6 4 6 2 4 4 2 6 2 4 4 2 4 4 3 5 1 9 4 6 2 4 4 2 6 4 9 1 5 3 5 1 9 4 6 2 4 4 2 6 4 9 1 5 1 9 4 6 2 4 4 2 6 4 9 1 9 4 6 2 4 4 2 6 4 9 4 6 2 4 4 2 6 4 6 2 4 4 2 6 2 4 4 2 4 4 以下是我目前掌握的代码:

#include <iostream>
using namespace std;

int a;
int p;
int set;

void numberss()
{
    for set[](int a=0; a<p; a++) 
} 

int main() 
{ 
    cin >> p;
    cin >> a;
    const int SIZE = p;
    int set[] = {a};
    int *numPtr;
    numPtr = set;
    for (int index = 0; index < SIZE; index++)
    {
        cout << *numPtr << " ";
        numPtr++;
    }

    for (int index = 0; index < SIZE; index++)
    {
        numPtr--;
        cout << *numPtr << " "; 
    }
    return 0;
}
#包括
使用名称空间std;
INTA;
INTP;
整数集;
void numbers()
{
对于集合[](int a=0;a>p;
cin>>a;
常数int SIZE=p;
int set[]={a};
int*numPtr;
numPtr=集合;
对于(int index=0;indexcout您的代码不起作用,因为您没有读取用户输入的所有数字,您只读取计数和第一个数字。此外,您没有循环足够的次数以三角形方式输出数字,您只输出三角形的第一行

请尝试以下方法:

#include <iostream>
#include <vector>
using namespace std;

int main() 
{ 
    int p;
    vector<int> set;

    cin >> p;
    set.resize(p);

    for (int i = 0; i < p; ++i)
        cin >> set[i];

    for (int index = 0; index < p; index++)
    {
        int *numPtr = &set[index];

        for (int i = 0; i < index; ++i)
            cout << "  ";           

        for (int i = index; i < p; ++i)
            cout << *numPtr++ << " ";

        for (int i = index; i < p; i++)
            cout << *--numPtr << " "; 

        cout << endl;
    }

    return 0;
}
#包括
#包括
使用名称空间std;
int main()
{ 
INTP;
向量集;
cin>>p;
设置大小(p);
对于(int i=0;i>设置[i];
对于(int-index=0;indexcout如果我们忽略错误,您可以一次读取一个数字,并在输出的第一行形成一个字符串。形成字符串需要在原始字符串后面附加一个反向副本。一旦形成字符串,您可以在第一行输出该字符串。然后用空格字符替换第一个数字,并从后面两个字符。继续这样做,直到你完成

这是因为数字都是个位数

int main (void)
{
    int N;
    std::string nums;
    std::cin >> N;
    for (int i = 0, x; i < N; ++i) {
        std::cin >> x;
        nums += std::to_string(x) + ' ';
    }
    nums.append(nums.rbegin() + 1, nums.rend());
    for (int i = 0; i < N; ++i) {
        std::cout << nums << '\n';
        nums[2*i] = ' ';
        nums.resize(nums.size()-2);
     }
}
int main(无效)
{
int N;
std::字符串nums;
标准:cin>>N;
对于(int i=0,x;i>x;
nums+=std::to_字符串(x)+'';
}
append(nums.rbegin()+1,nums.rend());
对于(int i=0;istd::cout试试看,伙计。我已经对它进行了评论,这样你就可以继续了。它允许使用数字以外的字符,因为这似乎不是你作业的要求,所以你有责任对这些字符进行排序和筛选。它按预期打印,所以希望这就是你在搜索r时要寻找的正确答案

#include <iostream>
#include <vector>
#include <string>

using namespace std;

void pop_front(vector<char>& _vector)
{
    _vector.front() = move(_vector.back());
    _vector.pop_back();
}

int main() 
{ 
    vector<char> characterList;
    string consoleInput;

    cin >> consoleInput;

    //Initialize by pushing everyting from the console into our vector.
    for (char c : consoleInput)
    {
        characterList.push_back(c);

    }

    //After adding from the console, now we'll do it in reverse.
    for (int i = consoleInput.length() - 1; i > -1; i--)
    {
        characterList.push_back(consoleInput[i]);
    }

    //Print where we are currently up to for display purposes.
    for (char c : characterList)
    {
        cout << c;
    }

    // Newline.
    cout << "\n";

    //Now let's start to chop it down. It'll take the same iterations as the console input, because we're doubling down.
    for (int i = 0; i < consoleInput.length() - 1; i++)
    {
        characterList.erase(characterList.begin());
        characterList.pop_back();

        for (char c : characterList)
        {
            cout << c;
        }

        cout << "\n";
    }

    return 0;
}
#包括
#包括
#包括
使用名称空间std;
void pop_front(向量和向量)
{
_vector.front()=移动(_vector.back());
_vector.pop_back();
}
int main()
{ 
矢量字符表;
字符串控制台输入;
cin>>控制台输入;
//通过将控制台中的所有内容推入向量进行初始化。
用于(字符c:控制台输入)
{
字符列表。推回(c);
}
//在从控制台添加之后,现在我们将以相反的方式进行添加。
对于(int i=consoleInput.length()-1;i>-1;i--)
{
characterList.push_back(控制台输入[i]);
}
//打印我们当前使用的位置,以便显示。
for(字符c:字符列表)
{

你没有示例代码吗?到目前为止你尝试了什么?欢迎来到StackOverflow:)请参阅有关如何提出有关StackOverflow的好问题的指南,人们可能会更倾向于提供支持。我们不是来为您做这项工作的,我们是来为您提供帮助的,当所有其他选项都消失时。这意味着一旦您进行了研究,一旦您尝试编写代码,一旦它被调试到您的能力和最佳状态n一些。-编辑。不是想让你气馁,只是想帮助你得到答案。:)到目前为止,我一直试图把数字倒过来,但首先界定数组中要使用的数字似乎太复杂了。到目前为止,我已经这样做了。请帮助。请参阅@davedwards对该主题的评论。请发布一个snippe您试图通过编辑在问题中写入的代码的一部分。您可以将其复制并粘贴到问题中,然后单击“代码格式”按钮。强烈建议您自己尝试;毕竟这是一个学习项目。我非常高兴给您举一个我将如何处理的示例,但我重申这是一个问题d你克服它将使你成为一名更好的程序员。我认为代码工作正常,但我使用(使用名称空间std;),因此我没有得到那么好的代码。我更新了我的答案,以显示你的原始代码的固定版本,该版本已损坏
#include <iostream>
#include <vector>
#include <string>

using namespace std;

void pop_front(vector<char>& _vector)
{
    _vector.front() = move(_vector.back());
    _vector.pop_back();
}

int main() 
{ 
    vector<char> characterList;
    string consoleInput;

    cin >> consoleInput;

    //Initialize by pushing everyting from the console into our vector.
    for (char c : consoleInput)
    {
        characterList.push_back(c);

    }

    //After adding from the console, now we'll do it in reverse.
    for (int i = consoleInput.length() - 1; i > -1; i--)
    {
        characterList.push_back(consoleInput[i]);
    }

    //Print where we are currently up to for display purposes.
    for (char c : characterList)
    {
        cout << c;
    }

    // Newline.
    cout << "\n";

    //Now let's start to chop it down. It'll take the same iterations as the console input, because we're doubling down.
    for (int i = 0; i < consoleInput.length() - 1; i++)
    {
        characterList.erase(characterList.begin());
        characterList.pop_back();

        for (char c : characterList)
        {
            cout << c;
        }

        cout << "\n";
    }

    return 0;
}