Menu
CoddyTech

사전 없이 조회하기

Coddy의 Rust 개발자를 위한 C 코스 레슨. 48개 중 30번째.

Rust의 HashMap은 저장 공간을 소유하며 Option을 통해 빌린 항목을 반환합니다. C17에는 내장 맵이 없으므로 표현 방식, 조회 결과가 없을 때의 처리, 반환된 포인터의 수명은 모듈 계약에 속합니다.

C에는 내장 맵이 없습니다. 명시적인 표현 방식, 조회 규칙, 값이 없을 때의 계약을 선택하세요.

Rust 예제


fn main(){
let stock=std::collections::HashMap::from([("pen",3)]);println!("{}",stock.get("pen").copied().unwrap_or(0));
}

Rust 출력:

3

C 예제

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stddef.h>
#include <stdbool.h>
#include <limits.h>
#include <errno.h>

int main(void) {
const char *names[]={"pen","book"};int counts[]={3,2};int result=0;for(size_t i=0;i<2;i++)if(strcmp(names[i],"pen")==0)result=counts[i];printf("%d\n",result);
return 0;
}

C 출력:

3
challenge icon

챌린지

쉬움

고정된 목록은 pen=3, book=2, ink=0입니다. stock_of(name)을 구현하여 개수를 반환하고, 알 수 없는 정확한 ASCII 이름이면 -1을 반환하세요.

잠금 해제된 소스 파일과 헤더 파일을 수정하세요. 표시된 잠금 상태의 main.c에서 함수를 직접 호출하므로 선언된 공개 API와 호환되도록 유지하세요. main을 추가로 만들지 마세요. 작업에서 범위를 명시적으로 확인하는 경우가 아니라면, 모든 숫자 작업 픽스처는 오버플로가 발생하지 않을 만큼 작습니다. 지정된 출력만 출력하세요. 드라이버가 표시된 경우 입력 파싱과 출력을 처리합니다.

직접 해보기

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stddef.h>
#include <stdbool.h>
#include <limits.h>
#include <errno.h>
#include "lesson.h"
int main(void){char text[128]="";if(fgets(text,sizeof text,stdin))text[strcspn(text,"\n")]=0;printf("%d\n",stock_of(text));return 0;}
quiz icon실력 점검

이 레슨에는 짧은 퀴즈가 포함되어 있습니다. 레슨을 시작해 문제를 풀고 진행 상황을 기록하세요.

Rust 개발자를 위한 C의 모든 레슨

직접 연습해 보세요: 온라인 C 컴파일러