在c ++中交换数组值

时间:2014-05-20 10:15:48

标签: c++ visual-c++

如果我的v = 4在[n]中,我想转移左数组值,从[n]中删除4,在结束索引加0,我想怎么做?

#include <iostream>

using namespace std;
const int n=5;
int main()
{

int a[n]={1,5,4,6,8}, v=4;
int b[n];

cout << "Enter a Value" << endl; 
cout<<v<<endl;

for(int i=0; i<n; i++){
 cout<<a[i];            
}
cout<<endl;
for(int j=0; j<n; j++){
    b[j]=a[j];
    if(a[j]==v)
    b[j]=a[++j];


  cout<<b[j];            
  }


 return 0;
 }

3 个答案:

答案 0 :(得分:3)

#include <vector>       // needed for vector 
#include <algorithm>    // needed for find
#include <iostream>     // needed for cout, cin 

using namespace std;

// Vectors are just like dynamic arrays, you can resize vectors on the fly

vector<int> a { 1,5,4,6,8 };  // Prepare required vector
int v;

cout << "enter value";  // Read from user
cin >> v;

auto itr = find( a.begin(), a.end(), v);  // Search entire vector for 'v'
if( itr != a.end() ) // If value entered by user is found in vector
{
    a.erase(itr);    // Delete the element and shift everything after element
                     // Toward beginning of vector. This reduces vector size by 1
    a.push_back(0);  // Add 0 in the end. This increases vector size by 1
}

for( int i : a )     // Iterate through all element of a (i holds element)
    cout << i;       // Print i
cout << '\n';        // Line end


一些有用的链接: vectorfinditeratorerasepush_back

答案 1 :(得分:2)

您可以使用std::rotate。我建议您使用std::vector而不是C数组,并充分利用STL算法。不过,下面我将介绍两个版本,一个是C数组,另一个是std::vector

带C数组的版本:

#include <iostream>
#include <algorithm> 

int main()
{
int const n = 5;
int a[n] = {1,5,4,6,8};
std::cout << "Enter a Value" << std::endl;
int v;
std::cin >> v;

for(auto i : a) std::cout << i<< " ";
std::cout << std::endl;

auto it = std::find(std::begin(a), std::end(a), v);
if(it != std::end(a)) {
  std::rotate(it + 1, it, std::end(a));
  a[n - 1] = 0;
}

for(auto i : a) std::cout << i<< " ";
std::cout << std::endl;

 return 0;
}

vector的版本:

#include <iostream>
#include <vector>
#include <algorithm> 

int main()
{
std::vector<int> a{1,5,4,6,8};
std::cout << "Enter a Value" << std::endl;
int v;
std::cin >> v;

for(auto i : a) std::cout << i<< " ";
std::cout << std::endl;

auto it = std::find(std::begin(a), std::end(a), v);
if(it != std::end(a)) {
  std::rotate(it + 1, it, std::end(a));
  a.back() = 0;
}

for(auto i : a) std::cout << i<< " ";
std::cout << std::endl;

 return 0;
}

答案 2 :(得分:1)

以下是使用std::array

的示例
#include <array>
#include <algorithm>

// defines our array.
std::array<int, 5> a = {{ 1, 2, 3, 4, 5 }};

// find the position of the element with the value 4.
auto where = std::find(a.begin(), a.end(), 4);

// if it wasn't found, give up
if (where == a.end())
  return 0;

// move every element past "where" down one.
std::move(where + 1, a.end(), where);

// fill the very last element of the array with zero
a[ a.size() - 1] = 0;

// loop over our array, printing it to stdout
for (int i : a)
  std::cout << i << " ";

std::cout << "\n";

为什么有人会使用这些笨拙的算法?嗯,有几个原因。首先,它们与容器无关。这将适用于数组和向量和deques,没问题。其次,它们可以很容易地用于同时处理各种元素,而不仅仅是单个项目,并且可以在容器之间复制等等。它们也是独立的...你有一个字符串数组,或一个int的向量,或其他更复杂的东西,算法仍然可以正常工作。

一旦你克服了他们最初的用户不友好状态,他们就会非常强大。

当然,您可以随时使用std::arraystd::vector或其他任何内容而不使用标准库算法。

std::array<int, 5> a = {{ 1, 2, 3, 4, 5 }};

size_t where = 0;
int to_remove = 4;

// scan through until we find our value.
while (a[where] != to_remove && where < a.size())
  where++;

// if we didn't find it, give up
if (where == a.size())
  return 0;

// shuffle down the values
for (size_t i = where; i < a.size() - 1; i++)
  a[i] = a[i + 1];

// set the last element to zero
a[ a.size() - 1] = 0;

作为最后一个例子,您可以使用memmove(由BLUEPIXY建议)在一个函数调用中执行随机播放操作:

#include <cstring>

if (where < a.size() - 1)
  memmove(&a[where], &a[where + 1], a.size() - where);