c_cpp emplace_back()和push_back()

- `emplace_back`和`push_back`都是向容器内添加数据<br/> <br/> - 对于在容器中添加类的对象时,相比于`push_back`,`emplace_back`可以避免额外的类的复制和移动操作<br/> <br/> - “emplace_back避免了使用push_back时所需的额外复制或移动操作。” <br/> <br/> - [std :: vector <T,Allocator> :: emplace_back](http://en.cppreference.com/w/cpp/container/vector/emplace_back)<br/> <br/> --------------------- <br/> <br/>版权声明:本文为CSDN博主「SpikeKing」的原创文章,遵循CC 4.0 by -sa版权协议,转载请附上原文出处链接及本声明。<br/> <br/>原文链接:https://blog.csdn.net/caroline_wendy/article/details/12967193 <br/>

back.cpp
#include <vector>
#include <string>
#include <iostream>
using namespace std;

struct President {
	string name;
	string country;
	int year;

	President(string p_name, string p_country, int p_year)
		: name(move(p_name)), country(move(p_country)), year(p_year) {
		cout << "I am being constructed.\n";
	}
	President(President&& other)
		: name(move(other.name)), country(move(other.country)), year(other.year) {
		cout << "I am being moved.\n";
	}
	President& operator=(const President& other) = default;
};

int main() {
	vector<President> elections;
	cout << "emplace_back:\n";
	elections.emplace_back("Nelson Mandela", "South Africa", 1994);

	vector<President> reElections;
	cout << "\npush_back:\n";
	reElections.push_back(President("Franklin Delano Roosevelt", "the USA", 1936));

	cout << "\nContents:\n";
	for (President const& president : elections) {
		cout << president.name << " was elected president of " << president.country << " in " << president.year << ".\n";
	}
	for (President const& president : reElections) {
		cout << president.name << " was re-elected president of " << president.country << " in " << president.year << ".\n";
	}
}

c_cpp 模板

template.cpp
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <functional>	// bit_xor
#include <iterator>
#include <map>
#include <numeric>		// accumulate
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;



int main() {

	return 0;
}

c_cpp 矢量➡集

vector2set.cpp
#include <iostream>
#include <set>
#include <vector>
using namespace std;

int main() {
	vector<int> v1 = { 1, 1, 2, 2, 3, 3, 3 };
	set<int> s1(v1.begin(), v1.end());
	for (auto val : v1) {
		cout << val << " ";
	}
	cout << endl;
	// 1 1 2 2 3 3 3
	for (auto val : s1) {
		cout << val << " ";
	}
	cout << endl;
	// 1 2 3
	return 0;
}

c_cpp IRrecvDumpV2.ino

IRrecvDumpV2.ino
//------------------------------------------------------------------------------
// Include the IRremote library header
//
#include <IRremote.h>

//------------------------------------------------------------------------------
// Tell IRremote which Arduino pin is connected to the IR Receiver (TSOP4838)
//
int recvPin = 11;
IRrecv irrecv(recvPin);

//+=============================================================================
// Configure the Arduino
//
void  setup ( )
{
  Serial.begin(9600);   // Status message will be sent to PC at 9600 baud
  irrecv.enableIRIn();  // Start the receiver
}

//+=============================================================================
// Display IR code
//
void  ircode (decode_results *results)
{
  // Panasonic has an Address
  if (results->decode_type == PANASONIC) {
    Serial.print(results->address, HEX);
    Serial.print(":");
  }

  // Print Code
  Serial.print(results->value, HEX);
}

//+=============================================================================
// Display encoding type
//
void  encoding (decode_results *results)
{
  switch (results->decode_type) {
    default:
    case UNKNOWN:      Serial.print("UNKNOWN");       break ;
    case NEC:          Serial.print("NEC");           break ;
    case SONY:         Serial.print("SONY");          break ;
    case RC5:          Serial.print("RC5");           break ;
    case RC6:          Serial.print("RC6");           break ;
    case DISH:         Serial.print("DISH");          break ;
    case SHARP:        Serial.print("SHARP");         break ;
    case JVC:          Serial.print("JVC");           break ;
    case SANYO:        Serial.print("SANYO");         break ;
    case MITSUBISHI:   Serial.print("MITSUBISHI");    break ;
    case SAMSUNG:      Serial.print("SAMSUNG");       break ;
    case LG:           Serial.print("LG");            break ;
    case WHYNTER:      Serial.print("WHYNTER");       break ;
    case AIWA_RC_T501: Serial.print("AIWA_RC_T501");  break ;
    case PANASONIC:    Serial.print("PANASONIC");     break ;
    case DENON:        Serial.print("Denon");         break ;
  }
}

//+=============================================================================
// Dump out the decode_results structure.
//
void  dumpInfo (decode_results *results)
{
  // Check if the buffer overflowed
  if (results->overflow) {
    Serial.println("IR code too long. Edit IRremoteInt.h and increase RAWBUF");
    return;
  }

  // Show Encoding standard
  Serial.print("Encoding  : ");
  encoding(results);
  Serial.println("");

  // Show Code & length
  Serial.print("Code      : ");
  ircode(results);
  Serial.print(" (");
  Serial.print(results->bits, DEC);
  Serial.println(" bits)");
}

//+=============================================================================
// Dump out the decode_results structure.
//
void  dumpRaw (decode_results *results)
{
  // Print Raw data
  Serial.print("Timing[");
  Serial.print(results->rawlen-1, DEC);
  Serial.println("]: ");

  for (int i = 1;  i < results->rawlen;  i++) {
    unsigned long  x = results->rawbuf[i] * USECPERTICK;
    if (!(i & 1)) {  // even
      Serial.print("-");
      if (x < 1000)  Serial.print(" ") ;
      if (x < 100)   Serial.print(" ") ;
      Serial.print(x, DEC);
    } else {  // odd
      Serial.print("     ");
      Serial.print("+");
      if (x < 1000)  Serial.print(" ") ;
      if (x < 100)   Serial.print(" ") ;
      Serial.print(x, DEC);
      if (i < results->rawlen-1) Serial.print(", "); //',' not needed for last one
    }
    if (!(i % 8))  Serial.println("");
  }
  Serial.println("");                    // Newline
}

//+=============================================================================
// Dump out the decode_results structure.
//
void  dumpCode (decode_results *results)
{
  // Start declaration
  Serial.print("unsigned int  ");          // variable type
  Serial.print("rawData[");                // array name
  Serial.print(results->rawlen - 1, DEC);  // array size
  Serial.print("] = {");                   // Start declaration

  // Dump data
  for (int i = 1;  i < results->rawlen;  i++) {
    Serial.print(results->rawbuf[i] * USECPERTICK, DEC);
    if ( i < results->rawlen-1 ) Serial.print(","); // ',' not needed on last one
    if (!(i & 1))  Serial.print(" ");
  }

  // End declaration
  Serial.print("};");  // 

  // Comment
  Serial.print("  // ");
  encoding(results);
  Serial.print(" ");
  ircode(results);

  // Newline
  Serial.println("");

  // Now dump "known" codes
  if (results->decode_type != UNKNOWN) {

    // Some protocols have an address
    if (results->decode_type == PANASONIC) {
      Serial.print("unsigned int  addr = 0x");
      Serial.print(results->address, HEX);
      Serial.println(";");
    }

    // All protocols have data
    Serial.print("unsigned int  data = 0x");
    Serial.print(results->value, HEX);
    Serial.println(";");
  }
}

//+=============================================================================
// The repeating section of the code
//
void  loop ( )
{
  decode_results  results;        // Somewhere to store the results

  if (irrecv.decode(&results)) {  // Grab an IR code
    dumpInfo(&results);           // Output the results
    dumpRaw(&results);            // Output the results in RAW format
    dumpCode(&results);           // Output the results as source code
    Serial.println("");           // Blank line between entries
    irrecv.resume();              // Prepare for the next value
  }
}

c_cpp 界()

bound.cpp
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

int main() {
	//                  0  1  2  3  4  5  6  7  8  9 
	vector<int> arr = { 1, 2, 2, 3, 3, 3, 4, 4, 4, 4 };
	cout << lower_bound(arr.begin(), arr.end(), 3) - arr.begin() << endl;	// 3
	cout << upper_bound(arr.begin(), arr.end(), 3) - arr.begin() << endl;	// 6
	return 0;
}

c_cpp 界()

bound.cpp
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

int main() {
	//                  0  1  2  3  4  5  6  7  8  9 
	vector<int> arr = { 1, 2, 2, 3, 3, 3, 4, 4, 4, 4 };
	cout << lower_bound(arr.begin(), arr.end(), 3) - arr.begin() << endl;	// 3
	cout << upper_bound(arr.begin(), arr.end(), 3) - arr.begin() << endl;	// 6
	return 0;
}

c_cpp 基本设定

basic_set.cpp
#include <iostream>
#include <set>
using namespace std;

int main() {
	set<int> set1;
	set1.insert(1);
	set1.insert(2);
	set1.insert(3);
	set1.insert(1);

	cout << set1.count(1) << endl;	// 1
	cout << set1.count(2) << endl;	// 1
	cout << set1.count(3) << endl;	// 1
	cout << set1.count(4) << endl;	// 0
	
	for (auto it : set1) {
		cout << it << " ";
	}
	cout << endl;
	// 1 2 3
	return 0;
}

c_cpp PIRセンサが反応したらLEDが光るArduino的サンプル

PIRセンサが反応したらLEDが光るArduino的サンプル

PIRSensor.ino
#define LED_OUTPUT 7
#define PIR_IN 2

void setup() 
{
  Serial.begin(9600);
  pinMode(PIR_IN, INPUT);
  pinMode(LED_OUTPUT, OUTPUT);
}

void loop() 
{
  Serial.println(digitalRead(PIR_IN));
  if (digitalRead(PIR_IN) == HIGH)
  {
    digitalWrite(LED_OUTPUT,HIGH);
  }
  else
  {
    digitalWrite(LED_OUTPUT,LOW);
  }
  delay(200);
}

c_cpp 完全背包问题

1d_array.cpp
#include <iostream>
#include <algorithm>
using namespace std;

int N, V;
const int MAX_N = 100 + 5, MAX_V = 1000 + 5;
int w[MAX_N], c[MAX_N], dp[MAX_V];

int main() {
	cin >> N >> V;
	for (int n = 0; n < N; n++) {
		cin >> w[n + 1];
	}
	for (int n = 0; n < N; n++) {
		cin >> c[n + 1];
	}
	// boundary
	for (int v = 0; v < V; v++) {
		dp[v] = 0;
	}
	for (int i = 1; i <= N; i++) {
		for (int v = w[i]; v <= V; v++) {
			dp[v] = max(dp[v], dp[v - w[i]] + c[i]);
		}
	}
	cout << *max_element(dp + 1, dp + V + 1) << endl;
	return 0;
}

/*
Sample Input:
5 8 
3 5 1 2 2 
4 5 2 1 3 
Sample Output:
16
*/

c_cpp 01背包问题

2d_array.cpp
#include <iostream>
#include <algorithm>
using namespace std;

int N, V;
const int MAX_N = 100 + 5, MAX_V = 1000 + 5;
int w[MAX_N], c[MAX_N], dp[MAX_N][MAX_V];

int main() {
	cin >> N >> V;
	for (int n = 0; n < N; n++) {
		cin >> w[n + 1];
	}
	for (int n = 0; n < N; n++) {
		cin >> c[n + 1];
	}
	// boundary
	for (int n = 0; n <= N; n++) {
		dp[n][0] = 0;
	}
	for (int v = 0; v < V; v++) {
		dp[0][v] = 0;
	}
	for (int i = 1; i <= N; i++) {
		for (int v = w[i]; v <= V; v++) {
			dp[i][v] = max(dp[i - 1][v], dp[i - 1][v - w[i]] + c[i]);
		}
	}
	for (int i = 0; i <= N; i++) {
		for (int v = 0; v <= V; v++) {
			cout << dp[i][v] << " ";
		}
		cout << endl;
	}
	cout << dp[N][V] << endl;
	return 0;
}

/*
Sample Input:
5 8 
3 5 1 2 2 
4 5 2 1 3 
Sample Output:
10
*/
1d_array.cpp
#include <iostream>
#include <algorithm>
using namespace std;

int N, V;
const int MAX_N = 100 + 5, MAX_V = 1000 + 5;
int w[MAX_N], c[MAX_N], dp[MAX_V];

int main() {
	cin >> N >> V;
	for (int n = 0; n < N; n++) {
		cin >> w[n + 1];
	}
	for (int n = 0; n < N; n++) {
		cin >> c[n + 1];
	}
	// boundary
	for (int v = 0; v < V; v++) {
		dp[v] = 0;
	}
	for (int i = 1; i <= N; i++) {
		for (int v = V; v >= w[i]; v--) {
			dp[v] = max(dp[v], dp[v - w[i]] + c[i]);
		}
	}
	cout << *max_element(dp + 1, dp + V + 1) << endl;
	return 0;
}

/*
Sample Input:
5 8 
3 5 1 2 2 
4 5 2 1 3 
Sample Output:
10
*/